Conflict with django-reversion package while adding custom button in django admin - python

i am customizing django admin so i added a custom button to a model admin by "extending admin/change_list.html" template
{% block object-tools-items %}
//add custom button here
{% endblock %}
it looks like this
when i added a new package django-reversion for versioning of models
which created new button for recovering data but repalced custom button which i created. I figured out that this is happening because package is also extending admin/change_list.html template and overiding object-tools-items block.
and i want some thing like this. Please help.

You were already told the answer when you raised this "bug" on the reversion Github.
Just extend the reversion/change_list.html template with your own custom
template. :)
Instead of making a template with the path admin/change_list.html make an overriding template with the path reversion/change_list.html. The reason for this is that reversion does some overriding of the templates it self, so you need to make sure to play nice with those.

Related

Define multiple templates for a predefined block wagtail CRX

I was moving a site over to wagtail and decided to use the codered extensions. The library comes with a image-gallery content-block. I want to use this but define a few templates you can choose from in the admin UI.
You usually define a template in the meta section, but I noticed a dropdown in the admin UI for a template.
How do I add a template to that dropdown? Link to the content block I want to change
I am interested in adding an HTML template and not inheriting from the content-block to change behaviour. (Unless inheriting is the only way to add a template to the dropdown.)
You could paramatise the path to the template you want to use then use an include in your block template to point to the chosen one.
For example, if you had a card block with selection for vertical or horizontal format. In your card block class you might have an property named template that uses a choice block, something like
class AlignmentChoiceBlock(ChoiceBlock):
choices=[
('blocks/flex/vertical_card.html', 'Vertical'),
('blocks/flex/horizontal_card.html', 'Horizontal')
]
Then in your block template, it just consists of:
<div class="some-block-container">
{% include value.template %}
</div>
Well, this works for Wagtail at least, not sure about codered.
The answer from Richard Allen works wagtail and is perfect for your own blocks etc. Wagtail blocks define a separate field that is used for their included components, for this you need another approach.
First you need to add the CRX_FRONTEND_TEMPLATE_BLOCKS to your django settings mysite/settings/base.py.
Then create a folder for your block templates in mysite/website/templates and create a custom template. Then add this path as a entry to the CRX_FRONTEND_TEMPLATE_BLOCKS. Entry key should be the block in lowercase. For a starter you could copy a template/html file from the codered package, found in coderedcms/blocks/
Now the template should be available from the template dropdown under the advanced menu of a crx block.
This info came from a gh issue of crx. This is e pretty recent addition and the dev mentioned that they are looking to make this easier. So this might change in the future, this worked for me on 26/01/2023.

show dynamic data from form value change in template

I'm new to django, i'm used to angular. i'm trying to do something that make sense to me in angular and I can't achieve in django.
I'm working with python 3.9 and django 4.1
I simplified my case to this..
I have a form that I created and and a view for it, i have a select element, whenever i select something, i want to show what i selected.
so I created a LocationForm form class:
class LocationForm(forms.Form):
apartment_type = forms.ModelChoiceField(queryset=ApartmentType.objects.all())
apartment type is just a list of apartment types (building, apartment, garden and so on)
i past the form to the view:
def location(request):
context = {'form': LocationForm}
return render(request, 'prospects/location.html', context)
and the code for the view:
{% load static %}
<form>
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>
CCC{{ form.apartment_type.value }}DDD<br/>
the problem is that no matter what I select, form.apartment_type.value still shows None, it's not being updated.
i guess i'm used to angular too much but this issue is really alarming for me if django only parses things statically, i guess i'm not used to it and i have no idea how to resolve it otherwise.
in my full code i want to reflect different type of items based of what chosen in the form but i can't do that if nothing gets updated.
any ideas how to resolve this?
any information regarding this issue would be greatly appreciated, i'm really lost here.
#update
it looks like i wasn't clear.
I want to understand the django template updates when variables are changed inside of it without refreshing the page.
only when i change the selection, without clicking save and sending the form i want to see the new value printed between CCC and DDD. currently i'm using {{ form.apartment_type.value }} but it stays None when i select an item.
if not.. how can i resolve this with django ?
i just said that i moved from angular and there it's like that out of the box and if here it's not, i hope there is a solution.
It seems that Django only compiles the pages when I render them first, if want to change anything in realtime I need to a javascript framework.
Found that at: https://stackoverflow.com/a/50189643/

Adding a link to the django admin panel

I have a page and I need to go to it from the admin panel. How can I specify a link in the admin panel? Maybe you can somehow add a link to the page in the header
Django's admin site provides a admin/base_site.html override point for exactly this purpose, and in fact the documentation for overriding templates talks about that template.
Make a copy of admin/base_site.html somewhere in your override hierarchy (see the linked documentation above) and modify the {% block nav-global %}{% endblock %} block to add the link you need.

Editing the details view of a model in flask admin

Is there a way to edit the details view model in flask-admin? I have searched the very bad docs and I couldn't find a way with which I would be able to display the model's details in a customized way!
Is there any reference to how the "details_view" should be used, assuming this is how I modify the details view?
If not, can anyone please explain to me how can I modify the way the info is displayed in that list? I have a "list of tags" column, and I wanna show tags separately based on certain criteria, I wanna apply some filters for example before showing them. How would I do that?
custom detail view can be acheived by :
1) setting the template of the modelView you are trying to customize :
class MyModelView(AdminModelView):
details_template = "admin/details.html"
2) Edit your custom template admin/details.html by totally overiding it with a whole new page. I guess you want to add additional information or custom fields most of the time, so you can start with a admin/details.html page that looks like :
{% extends 'admin/model/details.html' %}
{% block tail %}
{{ super() }}
<h1>My custom content.</h1>
{% endblock %}
By inheriting from parent template, you should have environment variable available in the template.
You may find more about available override options and such there : http://flask-admin.readthedocs.io/en/latest/api/mod_model/#flask_admin.model.BaseModelView.details_template

Django-Tables column templates with django permissions

So, I'm using Django-Tables to generate my project datatables, but now I'm facing a new problem.
I've got this Table Class to generate my Model datatables, using the DjangoTables app. Then I use the TemplateColumn to create a new column for base operations just like Edit, Copy, Delete... This stuff goes into the template that is loaded into the column of each row.
class ReservationTable(tables.Table):
operations = tables.TemplateColumn(template_name='base_table_operations_btn.html', verbose_name= _('Operations'))
So inside the template i've got this:
{% if perms.reservation.add_reservation %}
<span class="glyphicon glyphicon-paperclip"></span>
{% endif %}
So, using the django templates perms tags, is not working here but it does in to the normal django template.
Any tips on how can I handle those perms into this kind of template? I'm kinda losen.
Thanks in advance!
So, its not just like a "perfect answer" for this problem, but here is how I managed to solve this:
Instead of using in Template django permisions, I managed to setup the permissions in the route url config. Just by adding:
permission_required('permision_name',raise_exception=True)
Function in the url.py. So here comes the full url line:
url(r'^reservation/flight/add/$', permission_required('reservation.add_reservation',raise_exception=True)(FlightReservationCreate.as_view()), name='reservation-flight-create'),
This allow me to add perms to the View instead of filtering into the themplate view.
That's not a perfect solution, because its a different way to manage permissions, and the problem with the django-tables2 column template is still there.
By the way, the final result is the same for me, so its OK.

Categories

Resources