Reputation: 525
Is there any way to override admin templates with different files depending on Django version?
I've copied admin/index.hml
from Django admin templates, to achieve this: Django admin, hide a model, but there are changes in that template in different versions of Django, and sometimes the page breaks (specially with the deprecation of ADMIN_MEDIA_PREFIX
in Django 1.4)
I want to install my site in many servers with different versions of Django, but using just one unified code that notices the version automatically. Is that possible?
I've checked Django documentation, but I haven't found anything about this.
Upvotes: 1
Views: 542
Reputation: 122466
The ModelAdmin
class has various template settings that you can specify in subclasses. In Django 1.3 these are (at django.contrib.admin.options
, line 271):
add_form_template = None
change_form_template = None
change_list_template = None
delete_confirmation_template = None
delete_selected_confirmation_template = None
object_history_template = None
Similarly, AdminSite
has various template settings (at django.contrib.admin.sites
, line 35):
index_template = None
app_index_template = None
login_template = None
logout_template = None
password_change_template = None
password_change_done_template = None
You can override these for subclasses or specify them in a central location in your code (e.g., a __init__.py
file). For example:
from django.contrib.admin.sites import AdminSite
AdminSite.index_template = '...'
You can then vary the templates based on the current Django version. You may need to check what the code looks like in earlier Django versions as I haven't checked if all these variables are present in earlier Django admin code.
Upvotes: 2