overriding django template raises TemplateSyntaxError - python

I'm trying to reset the password_reset_email.html template in my django app by adding this file in templates/registration/password_reset_email.html:
{% load i18n %}{% autoescape off %}
{% blocktranslate %}You're receiving this email because you requested a password reset for your user account.{% endblocktranslate %}
{% translate "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% translate 'Your username, in case you’ve forgotten:' %} {{ user.get_username }}
{% translate "Thanks for using our app!" %}
{% blocktranslate %}The team{% endblocktranslate %}
{% endautoescape %}
However it raises a TemplateSyntaxError:
'blocktranslate', expected 'endautoescape'. Did you forget to register
or load this tag?
What am I doing wrong?

Related

jinja2 template expressions

Hy, i am very new to web dev and jinja2. i encountered a problem with jinja, when i render template and want to output user name to web page who loged in...well i cant get one. Here is my index.html file with jinja expression in tag:
{% extends "base.html" %}
{% block content %}
{% if user %}
<h1>Welcome to Flask authentication site, {{ user.name }}!</h1>
{% else %}
<h2>Hello, log in or sign up</h2>
{% endif %}
{% endblock content %}

Problem: jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endwith'

#app.route("/user")
def user():
if "user" in session:
user = session["user"]
return render_template("user.html", user=user)
else:
flash("You are not logged in")
return redirect(url_for("login")
This is the python code and below is the html
{% extends "base.html" %}
{% block title %} User {% endblock %}
{% block content %}
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for msg in messages %}
<p>{{msg}}</p>
{% endfor %}
{% endif %}
{% endwith %}
<p>Welcome, {{user}} </p>
{% endblock %}
Problem: jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endwith' ...and i cannot figure out what is the problem. Im pretty sure its something obvious.
Debugger shows me two problems: first in python codefile and the second in the user.html file.
And here is the traceback:
enter image description here

How to add images in email with django allauth

I have troubles with django allauth
Here's my template
password_reset_key_message.txt
{% autoescape off %}
{% load static from staticfiles %}
{% load i18n %}Hello{% if username %}, {{ username }}{% endif %}!
{% if username %}{% blocktrans %}In case you forgot, your username is {{ username }}.{% endblocktrans %}{% endif %}
You're receiving this e-mail because you or someone else has requested a password for your user account.
It can be safely ignored if you did not request a password reset. Click the link below to reset your password.
{% load static %}
<img src="{% static "avatar.jpg" %}" alt="avatar" />
{{ password_reset_url }}
{% if username %}{% blocktrans %}In case you forgot, your username is {{ username }}.{% endblocktrans %}
{% endif %}{% blocktrans with site_name=current_site.name site_domain=current_site.domain %}Thank you for using {{ site_name }}!
{{ site_domain }}{% endblocktrans %}
{% endautoescape %}
I'm trying to add image in email body (already trying with link on source and converting image to base64) but got same result
But when I click on reset_email I got this result:

Invalid block tag: 'shift_info', expected 'empty' ili 'endfor' Django

So, I've visited a lot of questions here on SO regarding Django block tags, but I was unable to learn from them in my perticular problem.
week_table.html
{% load base_extras %}
...
{% for shift in shifts %}
{% if shift.user %}
{% if user in shift.user.all or shift.user.count < shift.usernum %}
<td class="clickable" href="{% url 'termini:shift_add_remove' shift.id %}">{% if shift.name %}{{ shift.name }}<br>{% endif %}{% for usr in shift.user.all %}{% shift_info usr %}{% endfor %}</td>
{% else %}
<td>{% if shift.name %}{{ shift.name }}<br>{% endif %}{% for usr in shift.user.all %}{% shift_info usr %}{% endfor %}</td>
{% endif %}
{% else %}
<td class="clickable" href="{% url 'termini:shift_add_remove' shift.id %}">{{ shift.name|default:"" }}</td>
{% endif %}
{% endfor %}
The problem is I get Invalid block tag: 'shift_info', expected 'empty' ili 'endfor' error
shift_info is defined in base_extras.py
base_extras.py
from django import template
import re
register = template.Library()
#register.simple_tag
def user_info(user):
name = '%s %s' % (user.first_name, user.last_name)
if not name.strip():
name = user.username
return name
It's a simple case of typos and not looking at your own code...

How can i override django contrib comments template?

The django contrib comments form i'm using:
{% get_comment_form for post as form %}
<form action="{% comment_form_target %}" method="post">{% csrf_token %}
{% if next %}
<div><input type="hidden" name="next" value="{{ next }}" /></div>
{% endif %}
{% for field in form %}
{% if field.is_hidden %}
<div>{{ field }}</div>
{% else %}
{% if field.name == 'comment' %}
{% if field.errors %}{{ field.errors }}{% endif %}
<p
{% if field.errors %} class="error"{% endif %}
{% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}>
{{ field.label_tag }} {{ field }}
</p>
{% endif %}
{% endif %}
{% endfor %}
<p class="submit">
<input type="submit" name="post" class="submit-post" value="{% trans "Post" %}" />
</p>
</form>
After submitting the form, it redirects to http://127.0.0.1:8000/comments/posted/?c=..
That means it calls the template django/contrib/comments/templates/comments/posted.html
The content of django/contrib/comments/templates/comments/posted.html:
{% extends "comments/base.html" %}
{% load i18n %}
{% block title %}{% trans "Thanks for commenting" %}.{% endblock %}
{% block content %}
<h1>{% trans "Thank you for your comment" %}.</h1>
{% endblock %}
That doesn't extends my project's base.html.
I need to customize/override that template so that it extends my project's base.html. How can i do that?
If i can't do that, then if i upload my django web project on server, then how would i edit the content of django/contrib/comments/templates/comments/posted.html so that it looks like that:
{% extends "path/to/myproject/templates/base.html" %}
{% load i18n %}
{% block title %}{% trans "Thanks for commenting" %}.{% endblock %}
{% block content %}
<h1>{% trans "Thank you for your comment" %}.</h1>
{% endblock %}
On local pc, for this time i changed/edited the content of django/contrib/comments/templates/comments/posted.html hard-coded to extends my project base.html.
Can anyone give some idea to solve this? I have searched a lot to solve this.
Just override it in your project's "templates" directory:
<project_root>/templates/comments/posted.html
It doesn't seem to be well documented in either the comments app or Django's general template documentation, but it works the same as overriding admin templates (which is documented).

Categories