Why is Django blocktrans working one place and not another? - python

I have two Django templates (in a Pinax 0.9.x project) with text in a blocktrans block. One is working and the other isn't.
The one that's working looks like:
{% extends "site_base.html" %}
...
{% block body %}
<h1>ABC</h1>
<h2><em>DEF</em></h2>
<p>
{% blocktrans %}
GHI
...
The one that's not working is getting a debug mode error of:
TemplateSyntaxError at /JKL/
Invalid block tag: 'blocktrans', expected 'endblock' or 'endblock body'
It follows those headings by giving the following source code, with the blocktrans highlighted in red:
{% extends "site_base.html" %}
{% block head_title %}MNO{% endblock %}
{% block body %}
<p>
{% blocktrans %}
PQR
{% endblocktrans %}
</p>
Are these two blocks of code isomorphic? If they aren't, what is the difference? What should I be doing to have paragraphs with their content working in blocktrans tags?
Thanks,

You have to load the i18n template tags:
<!-- your_template.html -->
{% load i18n %}

Related

For loop indentation is not taking place in jinja2 templating in vscode

I am writing a for loop or if else using jinja2 templating but it is not giving me indentation in vscode. Fore example, I wrote this:
{% extends 'base.html' %}
{% block content %}
<h2>This is Item List</h2>
{% for item in items %}
<p>{{items}}</p>
{% endfor %}
{% endblock content %}
But when I press Ctrl s or save it manually, it loses the indentation. The code becomes like this:
{% extends 'base.html' %}
{% block content %}
<h2>This is Item List</h2>
{% for item in items %}
<p>{{items}}</p>
{% endfor %}
{% endblock content %}
This is happening for for loops, if else and everything inside jinja2 templating. Not happening in normal python codes.
This is not giving any error, but I feel distressed. How to solve it.
You need to download this extension to format the .j2 file https://marketplace.visualstudio.com/items?itemName=monosans.djlint

Nest {% block %} in {% for %} Django template inheritance

I am developing a dictionary application using Django. One of the main themes of the app is displaying feeds of definitions.
Specifically, I have:
an "index" feed where new definitions are listed,
a "headword" feed where definitions of a particular headword are listed,
and a "tag" feed where definitions tagged with a particular tag are listed.
index.html:
{% block body %}
<h1>Definitions</h1>
<ul>
{% for definition in definitions %}
// definition HTML
{% endfor %}
</ul>
{% endblock %}
headword.html:
{% block body %}
<h1>{{ headword }}</h1>
<ul>
{% for definition in headword.definitions_headword.all %}
// definition HTML
{% endfor %}
</ul>
tag.html:
{% block body %}
<h1>{{ tag }}</h1>
<ul>
{% for definition in tag.definitions_tag.all %}
// definition HTML
{% endfor %}
</ul>
Clearly, I need a base feed.html template that these three files can share. For the h1 tag no problem. However, I am having problems with the for loop iterable variables.
I tried nesting a {% block iterable %}{% endblock %} in the for loop of feed.html as in {% for definition in {% block iterable %}{% endblock %} %} and then plugging definitions, headword.definitions_headword.all, and tag.definitions_tag.all in {% block iterable %}, but Django does not seem to support that.
I could probably pass to all three templates a "definitions" context variable and use that as the iterable in all three for loops, but is there a way to do it without touching Python code?
Your explanation was a bit fuzzy to me at the end there, but going just based off your first sentence of "Clearly, I need a base feed.html template that these three files can share."
I think {% include %} may be what you are looking for.
https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#include
Create your base template of "feed.html" which includes generic variables. Generic variables below are "header_name" and "definitions".
<h1>{{ header_name }}</h1>
<ul>
{% for definition in definitions %}
// definition HTML
{% endfor %}
</ul>
In your parent templates (index, headword, tag), use the "include" tag. Pass in your parent template variables into the child template. Examples below:
headword.html
{% block body %}
{% include 'pathname/feed.html' with header_name=headword definitions=headword.definitions_headword.all %}
{% endblock %}
tag.html
{% block body %}
{% include 'pathname/feed.html' with header_name=tag definitions=tag.definitions_tag.all %}
{% endblock %}

Error adding a convential sign up and github sign up together in a django project

I am doing a django book store project which has a successful sign up function. I followed WS Vincent's tutorial to connect Github signup (https://learndjango.com/tutorials/django-allauth-tutorial) and I have come across an error when integrating it.
TemplateSyntaxError at /
Invalid block tag on line 16: 'provider_login_url', expected 'endif'. Did you forget to register or load this tag?
Below is the code for the major areas of my project.
home.html
{% extends '_base.html' %}
{% load static %}
{% block title %}Home{% endblock title %}
{% block content %}
<h1>Homepage</h1>
<img class="bookcover" src="{% static 'images/djangoforprofessionals.jpg' %}">
{% if user.is_authenticated %}
<p>Hi {{ user.email }}!</p>
<p>Log Out</p>
{% else %}
<p>You are not logged in</p>
<p>Log In |
Sign Up
Sign Up</p>
{% endif %}
{% endblock content %}
I am not familiar with HTML but I take the two sign up lines are not meant to be put one after the other.
According to the documentation, it seems like you forget to load {% load socialaccount %}.
Reference

How to fetch the entire table data and show them in the user interface in django using templates

I have defined a model called "Files" having fields as Id(auto genrated) and file_name.I want to fetch all the file names from the database and show them as a list on the UI.I am developing a django project.
I have added a read_files.html in the templates folder.
{% extends "home/base.html" %}
{% block title %}title{% endblock %}
{% block subtitle %}Read File{% endblock %}
<body>
<ul>
{% for f in Files %}
<li>{{ f.name }}</li>
{% empty %}
<li>Sorry, no files present in this list.</li>
{% endfor %}
</ul>
</body>
{% endblock %}'
But i am not able to see the correct results. Either it gives error
'Invalid block tag: 'endblock'' or 'incorrectly spelled text string endfor'
It seems you are missing a block opening tag after the subtitle block:
I added a {% block content %} tag (change "content" for the right block name
{% extends "home/base.html" %}
{% block title %}title{% endblock %}
{% block subtitle %}Read File{% endblock %}
{% block content %}
<body>
<ul>
{% for f in Files %}
<li>{{ f.name }}</li>
{% empty %}
<li>Sorry, no files present in this list.</li>
{% endfor %}
</ul>
</body>
{% endblock %}
also don't add extra characters after you finish a block if you are extending a template

Django: HTML tags displayed as text

In my Django application, the <acronym>...</acronym> tag is displayed as <acronym title='Особо охраняемая прирордная территория'>ООПТ</acronym>.
The template looks like this (the acronym tag appears in cur_goal.description).
{% extends "base.tpl.html" %}
{% block title %}{{ genplan.name }}{% endblock %}
{% block content %}
<h2>{{ category_name }}</h2>
{% for cur_goal in goals %}
<li>{{ cur_goal.description }}</li>
{% endfor %}
{% endblock %}
{% block right_sidebar %}
{% endblock %}
How can I change the code so that the acronym tag is displayed correctly (the braces are not replaced by < and >) ?
As DM says, autoscape tag, or... the safe filter
<li>{{ cur_goal.description|safe }}</li>
Or mark_safe from the python side.
https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.safestring.mark_safe

Categories