I am using ModelAdmin to register a custom model with the Wagtail admin. I would like to customise the IndexView. The only thing I want to do is add a button to each row. I can see that the rows are added by the template tag - results_list, which leads me to the results_list.html in modeladmin/includes.
As this is registered with the #register.inclusion_tag decorator, is it possible to override the template in my own app? The normal django methods of overriding templates dont seem to be working for me...
You can customise the buttons for the index page by setting a custom button_helper_class on your ModelAdmin class: http://docs.wagtail.io/en/v1.8.1/reference/contrib/modeladmin/primer.html#modeladmin-button-helper-class
(Incidentally, the tag and template are named result_list, not results_list - perhaps that's why you weren't able to override it?)
Related
I have two admin sites in my django app. One named admin and the other named owneradmin.
I want to override their templates separately. Is there a way to do it ?
If you want to override the index template, the easiest way is to set index_template for the AdminSite.
CustomAdminSite.index_template = 'path/to/custom/template.html'
OwnerAdminSite.index_template = 'path/to/owner/template.html'
See django docs for more details about index template and template overriding.
A curious question
Suppose I have a module/app say Demo which contains a model say "anything". Now I registered this model to admin.py.
Now when I log in to admin and see this model I see general update, delete option in admin template.
Is it possible that when I see this model in admin and click that model it redirects to another url that is of that module's url and it display in another template and I can show anything I like from view..
I am sorry if it is confusing. What I want is I want to show the admin's model property like update, add and delete in another custom template made by me and it should be redirected to url defined in the module's urls.py
Thanks in advance.. :)
This can be done by extending your base get_urls() method docs here
I have a django and I wrote some views. I know how to pass my variables to template, but I also has some external modules with their own views, which I wont modify. Please help me understand how can I get one of my object "Menu.objects.all()" exist in all templates? So for example a I have django-registration and i want to have all my menu items appear at top when someone visits not my app url. I mean it will be registration app url, which returns templateresponse (and here I dont have my variable).
You can add variables to context
The cleanest way to do it, is to use a Template Context Processor, which is a hook that will allow you to add things to your context before the template is rendered.
http://www.djangobook.com/en/2.0/chapter09.html
https://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors
Here's an example of Template Context Processors used in the django core:
https://github.com/django/django/blob/master/django/template/context_processors.py
In Django admin,
Is it possible that dynamically sort by field name using url param?
In this way,
https://www.example.com/admin/blog/article/?order=modified_date
https://www.example.com/admin/blog/article/?order=deleted_date
Depending on your version of Django, you can use the get_ordering() method in your ModelAdmin class.
I'm using django-registration. I'd like to update the AuthenticationForm in 'django.contrib.auth.forms'. Specifically, I would like to pass the 'attrs' dict to add a few attributes. How do I update a form that comes with django?
You can use the views that come with auth and override the form parameter with your own form:
django.contrib.auth.views.login(request[, template_name, redirect_field_name, authentication_form])
More info here.
The standard way is to subclass AuthenticationForm, change the attrs in constructor, pass the form to login view and write a new entry in urls.py.
This is a nightmare: in order to add html attribute to a field it is necessary to use python subclassing, to know how exactly django's form metaclass work (self.fields['field'].widget.attrs, not just self.field.widget.attrs), to know regexes (for urls.py), to know how django's urls.py work (should you put the overriding line before of after include('django.contrib.auth.urls')?) and to know where is the auth form and auth view imported from.
And now the "commercial break": just use http://pypi.python.org/pypi/django-widget-tweaks for your task ;)