Multiple Django template loaders - python

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.

Related

Django override custom admin site's templates

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.

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.

How to render Bootstrap admin template?

I have this admin panel template and I want to use it in my Django web application. I know I will have to modify the template using template tags but is that all? What all changes will I have to make. I am fairly new to Django and front end development.
It is pretty easy, indeed to work with Django and front-end. Answering your question: Yes, you have to change some more things to the template. You have to change links to something like
<link href="{% static 'templates/my_app/css/style.css' %}>
Also, you have to be quite careful with the views and Models python files. The views will determine what will your administrative system will do, using the models.

Django widget template override does not search in the project template directory. How to fix?

I am attempting to override a built in widget template in Django 1.11. I seem to be doing everything that the docs say to do in this regard, but for the widget templates, Django is not looking in my project at all, and I get a TemplateDoesNotExist error.
Here's what I have for the override:
class MyFileWidget(widgets.FileInput):
template_name = 'myapp/my_file_widget.html'
The template is definitely there. If I pass the template to a render call, it finds it fine. Problem is an issue of paths. When calling render from a view, it checks the following:
projectroot/templates/myapp/my_file_widget.html
djangoroot/forms/templates/myapp/my_file_widget.html
When it finds the template in my project, it renders it. This is NOT happening when I provide the template path in the class above. In that case, it does not check in my project templates, where the file actually exists, and begins checking in the django path, where it does not. Hence the error message.
So I have no clue why this the loader would check my project templates on render calls, but then fail to do so when looking for the "template_name" of the widget override. Any ideas?
By default, the FORM_RENDERER setting defaults to 'django.forms.renderers.DjangoTemplates'.
This checks your apps' templates directory (e.g. projectroot/myapp/templates/myapp/my_file_widget.html), but it does not check your project's template directories (e.g. projectroot/templates/myapp/my_file_widget.html).
If you want to use the same configuration as your TEMPLATES setting, then you should use the TemplatesSetting renderer.
FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'
You will also need to update your TEMPLATES setting so that Django can still use the built in widget templates. The easiest way to do this is to add django.forms to INSTALLED_APPS. See the docs for more info about this.

Custom django admin template for app

I would like to create a custom index.html derived from the admin/index.html individual for each app in my django project.
For instance my folder structure is like:
app1
templates
index.html (different from the global template admin/index.html)
app2
templates
admin
base.html
index.html (global template index.html)
How can I achieve custom admin index.html files for my apps, that are recognized by django? For the moment only the index.html in the global template/admin folder is considered for rendering the index pages in my backend.
I'm using django 1.6
Unfortunately, only certain parts of the Django admin site can be overridden on a per-app basis, as it says in the documentation:
Not every template in contrib/admin/templates/admin may be overridden per app or per model. The following can:
app_index.html
change_form.html
change_list.html
delete_confirmation.html
object_history.html
Remember that the admin interface is itself and app, so it's going to do a single template sweep and load the first set of templates that comes up.
I think your two best bets are either to use multiple admin sites in your project or to add a custom view for specific apps -- the former is probably easier, but will be a problem if you don't want people to have to login separately to control certain things.

Categories