Editing the details view of a model in flask admin - python

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

Related

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/

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

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.

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.

Advice about composing several django templates

I'm developing a web application with Django and we have met a dilemma about the design.
We were making one template for every screen, but right now we have detected that some parts of of the screen with the information are repeated all over the different screens. For example, when coming to show personal data of a person, you can show also another data concerning that person, but not personal data (financial data, for instance).
My intuition told me that we should look for a solution in which we could make small templates that we could compose, combine and concatenate and that we should also make different views or functions which would return its own associated template each one.
Thus, person_data() would return the rendered template showing the name, surname, address, etc... and financial_data() would return the rendered template showing the salary, bank account, etc... After that, the desirable thing would be concatenating both or inserting them in a wider template for showing all this together.
<html>
...
{# Html code here #}
...
{# person_data template #}
...
...
{# financial_data template #}
...
{# Html code here #}
...
</html>
So as always I made some research on the net and I found:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include
a link which describes how to use include for inserting a template file in another template:
{% include "foo/bar.html" %}
And you can also use a variable from the python function with the path name or an object with render method:
{% include template_name %}
So we could use all this in this way for combining, concatenating, composing and operating on templates. We could choose the one which includes the others and passing it the context for all templates (I suppose, I didn't test anything). But I don't know if I'm well directed or if this is the best way to go. I would appreciate some advice.
I also found another interesting thread in stackoverflow talking about this:
How do you insert a template into another template?
My idea is to have small templates which are used repeatedly in different spots of the web and composing them in Unix-like style, so I would have small visual pieces that would be used once and again saving a lot of hours of writing of code.
Please some advice.
Thanks in advance
Yes, that is a correct approach. I think the best solution is to combine {% include %} and {% extend %}, which will allow you to inherit from a base template (via extend), and include parts you want from other templates (via include).
You'd end up having a base template, a template for the header, the footer, parts of the body etc.
You might also want to read more about it here and here

Django: Custom 'clean' method on a Form that sets to self._errors, but isn't shown on the HTML?

I have a Form in Django, I've added a custom clean() method since some of the fields depend on each other. When there's an error, I add things to self._errors['FIELDNAME'] (as the Django documentation says).
However I also need to customize the output of this form in the HTML, so I'm manually writing HTML, and using {{ form.FIELDNAME }} etc. However I can't get the error message for that field. form['FIELDNAME'].errors is empty (i.e. {{ form.FIELDNAME.errors }} in the termplate), but form.errors['FIELDNAME'] has the error message.
What's the Right Way™ to do this? Should I access {{ form.errors.FIELDNAME }} in the template, or should I set the errors in the clean method another way? Is this a bug in Django?

Categories

Resources