Can I use {% static %} outside the base.html in Django? - django

I am a beginner in Django and i have setup a project with a base.html file along with the folder static/images/img.jpg.
base.html is setup correctly. Everything works inside this file but i have another file called index.html in the templates folder.
So, in other words in the base.html i can use the command {% static '/images/img. jpg' %} but in my index.html Django does not recognise the command.
In the index.html I have already provided the required commands {% extends 'base.html' %} and {% load static %}
Does this command work only in the base html? Do i need another command for files outside the base.html
Thank You

This command should work on every template file of Django, take a look if you are putting the code between a block {% block %} {% endblock %} or even if your IDE don't autocorrect it, run it and see if it works correctly

Related

Django template does not exist include or extends tags

I am creating a Django project. My question is simple I believe but I am having a tough time trying to figure it out. I have an index.html in which I want to render certain things. I decided that I don't want to have a html header for every one of my html pages which to me is insane. I created a base.html file to contain a html header and I want to reuse this for my other html files but how do I do this exactly, that is if it's possible.
So what I am mostly not sure about is which tag to use {% include "base.html" %} or {% extends "base.html"%}. Intuition is telling me I should use {% include "base.html" %} because I don't believe I should be inheriting. And then my second question which relates close to this is how do I even include or extends. I just created a base.html file and an index.html file and have a index view in my views file which renders the index.html with context.
Everything works fine if I were to just use the index.html file but when I do {% include "base.html" %} in my index.html file it does not work and error says temlate base.html does not exist. Same goes if I were to use extends rather than include
I don't want to have a html header for every one of my html pages
Define a block which you can define in other html pages as you need, like this:
In base.html:
{% block header %}{% endblock %}
In index.html:
{% block header %}
{% include "header.html" %}
{% endblock %}
In page_without_header.html:
// avoid defining the header block

'zinnia_tags' is not a valid tag library

I am trying to override the zinnia templates in my django project. I have placed the file base.html at \templates\zinnia\ with the following contents:
{% extends "base.html" %}
{% load zinnia_tags i18n %}
{% block sidebar %}
Change.
{% endblock %}
But when I visit \weblog, I get the error :
TemplateSyntaxError at /weblog/
'zinnia_tags' is not a valid tag library: Template library zinnia_tags not found, tried django.templatetags.zinnia_tags,django.contrib.staticfiles.templatetags.zinnia_tags,django.contrib.humanize.templatetags.zinnia_tags,django.contrib.admin.templatetags.zinnia_tags,endless_pagination.templatetags.zinnia_tags,haystack.templatetags.zinnia_tags,notifications.templatetags.zinnia_tags,links.templatetags.zinnia_tags,allauth.account.templatetags.zinnia_tags,allauth.socialaccount.templatetags.zinnia_tags,django_comments.templatetags.zinnia_tags,mptt.templatetags.zinnia_tags,tagging.templatetags.zinnia_tags,zinnia.templatetags.zinnia_tags
I have tried this based on the idea mentioned in their mailing list
I have loaded the zinnia template dir in my settings.py. I am unable to figure out why I get the error.
Thanks for your answers in advance.
Due to https://github.com/Fantomas42/django-blog-zinnia/blob/f7b32fe49ecc365b13f20e8557b3a1cb6fb0df20/CHANGELOG#L25, you need to import zinnia, and not zinnia_tags.

specify staticfiles to be loaded in every django template?

Is there a way to do {% load staticfiles %} automatically in every template file?
Would it significantly affect performance?
If it's a bad practice, I wonder if the following use-case is ok.
I'm *include*ing a file in a loop, and loading staticfiles,
{% for a in a_list %}
{% include "a.html" %}
{% endfor %}
a.html
{% load staticfiles %}
use {% static "a.html" %}
You can have the static tag automatically loaded into the set of default tag available in a template using the add_to_builtins method from template.base:
from django.template.base import add_to_builtins
add_to_buildins('django.templatetag.static')
This code would probably be best placed in your settings.py file, or anywhere else that gets imported automatically.

Django - Dynamically include JS/CSS based on installed apps

Does anybody know a way to adjust the included JS/CSS resources in a template based on the apps you've installed?
Let's say we have a basic feature in app x using template.html, and this requires foo.js which is provided in the static files for the app.
What I'd like is a way of saying an additional and optional app y can register bar.js to be included in template.html as well and this provides some advanced functionality.
Ideally, this should be tied in on a feature level - so I register both foo.js and bar.js to provide for feature A and in my template I just indicate I want all the static content for A.
You can follow the django admin framework approach. In your base template have an extra section for style and javascript. Based on some condition you can insert the new files.
For Example:
Define these two blocks in your base template
{% block extracss %}{% endblock %}
{% block extrajs %}{% endblock %}
If you want to add a js or css based on some condition, you can add a check inside
{% block extracss %}
{% if new_app_installed %}
# Insert your CSS
{% else %}
# Default
{% endif %}
{% endblock %}
You can also check if your plugin app is installed and pass this context variable from view to template.
from django.conf import settings
if "new_app" in settings.INSTALLED_APPS:
is_new_app_installed = True

Django Tutorial: Templates and Views

I'm in the process of completing the official Django tutorial and I'm stuck on part 3. Since templates are also used in the last part of part2, I will describe what I did:
Part 2 told me to "copy the template admin/base_site.html from within the default Django admin template directory in the source code of Django itself (django/contrib/admin/templates) into an admin subdirectory of whichever directory you're using in TEMPLATE_DIRS."
So I created a new directory "admin" that has the following relative path (note that where Django uses the directory name 'mysite', I use 'django_test' : /django_test/polls/templates/admin. I copied the base_site.html file into this directory.
When I render the file in my local browser, it says: {% extends "admin/base.html" %} {% load i18n %} {% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %} {% block branding %}{% trans 'Django administration' %}{% endblock %} {% block nav-global %}{% endblock %}
Part 3 has me create an index.html file in a new subdirectory polls/index.html. But when I load this file in my web browser (using localhost server), I simply see the html code instead of a bulleted list (see below).
Note that I also edited TEMPLATE_DIRS in my settings.py file to tell Django that it can find index.html under /Users/myname/Sites/django_test/django_test/templates
Below I will paste the code that my local server renders (instead of the bulleted list, which is what I want). Do you know why this code is being rendered, instead of the bulleted list?
<html>
<head><title>Test</title></head>
<body>
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li>{{ poll.question }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
</body>
</html>
I don't know whether I'm making a mistake in how I'm organizing the files. Might someone have an idea about what I'm doing wrong?
As you say in your comment, you're putting the file path into your browser. Naturally, then, you're going to see the text of the template, because you are bypassing Django completely and getting the browser to load the unrendered template from disk.
As the tutorial describes, you need to ask Django to serve the template and render it, via its normal URL mechanism. In the earlier part of that section, you went to localhost:8000/admin/ to see the admin site - this hasn't changed just because you've replaced a template. Go back to that address and you'll see your updated - and rendered - template.
The django admin site is easy once you get the hang of it.
The steps to take are:
-Uncomment the django admin site in your urls.py
-Make the css available to the admin site by either copying the admin folder (inside django package) into the folder specified in STATIC_ROOT in your settings.py or making the diectory available on your PYTHONPATH
In other words, you dont need to create a template for the admin site. You will, however, need to create templates to access the views that you create in your project