Django override custom admin site's templates - python

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.

Related

Overriding a template in Django

I'm using this library to add a sessions tab on my django project. I'm calling the module's template from my project using this line:
<li>Sessions</li>
It works, but now I'd like to style the page, and to do that I need to override the module's template with my own template. So, as the module says here, I inherited the module's SessionListView on my own views.py to overrode the template:
from user_sessions.views import SessionListView
class MySessionList(SessionListView):
template_name = 'user_sessions/session_list.html'
Then I added the url to my urls.py
url(
regex=r'^account/sessions/$',
view=MySessionList.as_view(),
name='sessions',
),
And then I created my template on my own project which is located like this:
templates/user_sessions/session_list.
But for some reason, I still can't see MY template appearing, the module's template keeps appearing instead. Can anyone help me finding what I'm doing wrong?
The SessionListView already uses user_sessions/session_list.html by default, so your custom view isn't required.
It sounds like Django is finding the user_sessions/session_list.html template in the user_sessions app before your template. If your template is in an app's templates directory, then you can fix this by moving your app above user_sessions in your INSTALLED_APPS setting.
Alternatively, if you move your template to a directory in the 'DIRS' list in the TEMPLATES setting, then Django will find your template before it checks the app templates directories. This is cleaner if your overridden template doesn't really belong to any of your apps, and another advantage is that you don't have to re-order the INSTALLED_APPS list.
See the docs on overriding templates for more information.

Override template tag in wagtail cms?

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?)

How to use the Django admin template for a form?

I want to use the existing Django Admin template for a form of my own. The situation is like there are two models. say A and B and I want a form that displays the details about both the models in a songle form of my own. So I am using a ModelForm. But I want to use the templates from the django admin. How can I do that?
I think its better to override the admin interface.
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates
Would a Class-based generic view work for you?

Dynamically sort in Django admin

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.

Multiple Django template loaders

I'd like to use Jinja2 with a Django project. I'm using this template loader from Django Snippets, but I still need to be able to render templates with Django for the admin pages. Whenever I try to use the admin pages, though, I get a TemplateSyntaxError because of the unknown tag "load". Is there a way to make the Jinja template loader defer to Django's built-in system when it can't handle the template?
I would recommend using this fork of Coffin when using Jinja and Django templates at the same time: https://github.com/GaretJax/coffin/blame/master/README.rst I implemented this on http://umbel.com/ which uses a combination of Django and Jinja templates.
It has a setting that you can use to disable Jinja's template loader for specific apps:
JINJA2_DISABLED_APPS = (
'admin',
'auth',
)
Hope that helps you out.

Categories