Django: nested template commands - django

I am following the tutorial of Tango with Django. When I came to this part of writing nested block template commands in about.html:
{% extends 'rango/base.html' %}
{% block title %}About{% endblock %}
{% block bodyBlock %}
<img src="{% static '/rango/images/pythonDjango.jpg' %}">
{% endblock %}
The system complains with the error message: Invalid block tag: 'static', expected 'endblock'
How should the above nested commands be arranged?

The static tag is not loaded into the template. So, at the top of the template, add:
{% load static %}
Everything else with the posted template looks fine.

Related

Purpose and use of Wagtail/Django template tags of the form {% block body_class %}template-homepage{% endblock %}

In the Wagtail docs (Your first Wagtail site) we are told to create a homepage.html template as follows:
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block body_class %}template-homepage{% endblock %}
{% block content %}
{{ page.body|richtext }}
{% endblock %}
In the above code, what is that tag {% block body_class %}template-homepage{% endblock %} supposed to be doing? I can't find any mention of it in the text of the tutorial.
The tutorial goes on to instruct us to create two more templates, blog_index_page and blog_page templates, both of which are to contain {% block body_class %} tags. Again, I can find no mention at all of those lines in the docs, let alone an explanation of how they might be used/modified.
A search for "body_class" in the docs finds only the three code blocks just mentioned, and one other mention: in the version 1.2 release notes, under 'What's new — Minor features' the notation: "Simplified body_class in default homepage template."
Any insight as to what I'm supposed to do with these tags? What special meaning or function, if any, does the text enclosed within the tags in those tutorial templates (for example, "template-homepage" in the code above)?
In base.html you have a part that looks like <body class="{% block body_class %}{% endblock %}">. What you add into that block in your templates will be rendered into that section.
For example, lets say on your design your <body> contains a bottom margin, however on a particular template you don't want the margin there and create a css class .remove-margin { margin-bottom: 0 };
You can then add {% block body_class %}remove-margin{% endblock %} into your template and it will apply the class to the <body> tag without you having to modify the base.html template for that one use case.
You are also able to create your own ones. In base.html add a custom block such as {% block my_custom_block %}{% endblock %} and then in your template include some text or html inside that my_custom_block and it will be rendered wherever you've place that part in the base template.
Just to help clarify the correct answers above with a picture as I was also wondering what that block tag was doing, here is the rendered html. As per answer above, this is standard Django.
In the base.html you have this line:
<body class="{% block body_class %}{% endblock %}">
The substitution happens in the home_page.html template:
{% block body_class %}template-homepage{% endblock %}
And it renders as per below screenshot:
<body class="template-homepage">

Static template tag for Django template engine

I have the following code:
<img src="{% static 'images/{{i.sideid.sidepic}}' %}"/>
But this doesn't load the picture...
However, if I change the {{i.sideid.sidepic}} to the picture name "republic.png" it works tho. So, yeah, {{i.sideid.sidepic}} is actually the exact same name ("republic.png"), because I do a print in django views and shows it in my cmd, the exact same name "republic.png".
I guess there has to be a specific way to add that {{i.sideid.sidepic}} inside the jinja {% %} .
You should be able to concatenate strings with the add template filter:
{% with 'images/'|add:i.sideid.sidepic as image %}
{% static image %}
{% endwith %}
N.B. The variable directly following the as can be anything you want:
{% with 'images/'|add:i.sideid.sidepic as sidepic %}
{% static sidepic %}
{% endwith %}
What you are trying to do doesn't quite work with the static template tag because it takes either a string or a variable.
Hope that helps!

Django load specific javascript file

I would like to load a specific javascript file for the specific html file. Instead of loading them into the base.html. Then a lot of pages will have unnecessary scripts that are not used.
I get this error:
Invalid block tag on line 3: 'static', expected 'endblock'. Did you forget to register or load this tag?
when i try to load it into the child.html. I'm
{% extends 'base.html' %}
{% block js %}
<script src="{% static "javascript/week.js" %}" type="text/javascript"></script>
{% endblock %}
You need to do {% load static %} in any template file that uses the {% static %} tag.

Django conditional javascript file in template causes missing staticfile error in production only

I have a conditional in a template used by several views which will include a js file if passed in by the view:
in the template:
{% if js_file %}
{% block inner_js %}
<script defer type="text/javascript" src="{% static js_file %}"></script>
{% endblock inner_js %}
{% endif %}
which is eg used by a view by:
.......
context = {'title': 'Sign up',
'form': form,
'js_file': 'js/supplier.js'}
return render(request, 'pages/signup.html', context)
This produces no errors in development, but when pushed to production I get the error:
ValueError at /users/example/
Missing staticfiles manifest entry for ''
If I remove the template if block above then this error dissapears (and works fine for those views which use the template without a js file).
I would rather not have to create a new version of the template for every view which uses a js file, is their a better way to fix this? (seems a bit of a weird error).
I don't believe you can conditionally include/exclude a block as you're doing. If you put the if tags inside the block, it will only include the <script> tag when the variable js_file is populated.
If you're conditionally including this block to override another block called inner_js in a template higher up, you can do something like this to achieve the same results:
{% block inner_js %}
{% if js_file %}
<script ...></script>
{% else %}
{{ block.super }}
{% endif %}
{% endblock inner_js %}
{{ block.super }} is the equivalent of calling Python's super in a class, and allows for block extensions in templates that extend a base.

Django: 404.html exists but 500.html is used

http://127.0.0.1:8000/app/slug/
With Debug=True I get Page not found (404) - No entry found matching the query. With Debug=False I get shown the projectName/templates/500.html instead of 404.html.
Both look exactly the same. 500.html:
{% extends "base.html" %}
{% block title %}server error{% endblock %}
{% block content %}
<h3>Error 500: server error</h3>
{% endblock %}
404.html:
{% extends "base.html" %}
{% block title %}page not found{% endblock %}
{% block content %}
<h3>Error 404: page not found</h3>
{% endblock %}
Why does Django load 500 instead of 404 although it exists? It cannot be a template error.
If you're 100% sure that you've correctly set up ALLOWED_HOSTS in your settings, chances are your 404.html template is somehow not correct. There might be a syntax error (or it can't find the base template), which, in turn, causes a new 500 exception to be raised.
Try simplifying your 404.html by only including static HTML.