Django Messages, How to Hide Specific Ones - django

I am using Django's messages framework to indicate successful actions and failed actions.
How can I exclude account sign in and sign out messages? Currently, landing on a page after signing in displays
Successfully signed in as 'username'. I do not want this message to be displayed, but all other success messages should be displayed. What I attempted is shown below. I tried using logic to find if the message had the word "signed" in it. If it did, do not display it. That however is not working though.
{% if messages %}
<div class="db-section">
<ul class="messages">
{% for message in messages %}
{% if "signed" in message %}
# Don't display anything
{% else %}
<div class="alert alert-error">
<strong style="color:{% if 'success' in message.tags %}green{% else %} red {% endif %};padding-bottom:10px;">{{ message }}</strong>
</div>
{% endif %}
{% endfor %}
</ul>
</div>
{% endif %}
Could someone possibly explain why the above code is still displaying messages that even contain "signed" in it?

Use safe filter to convert that message object attribute to actual string and compare
--- Your code ---
{% if "signed" in message|safe %}
# Don't display anything
{% else %}
--- Your remaining code ---

Related

mailgun for loop doesn't render email correctly

I have copied for loop example from mailgun documentation page and trying to make it work, but it doesn't recognize as a code
{% for item in items %}
<span>{{ item }} </span>
{% endfor %}
the above code renders as
{% for item in items %} test_item {% endfor %}
in the email when it is sent or reviewed in the email review.
Do I need to enable something? {{name}} by itself works ok, loop doesn't work

Do errors in BoundField.errors and BoundField.help_text need to be escaped in a template in Django?

Do errors in BoundField.errors and BoundField.help_text need to be escaped in a template in Django? My guess is yes because both the errors and the help_text are no place for HTML code. However, I am a bit confused after I saw the following two snippets of code in the documentation of Django.
Snippet A:
{% if form.subject.errors %}
<ol>
{% for error in form.subject.errors %}
<li><strong>{{ error|escape }}</strong></li>
{% endfor %}
</ol>
{% endif %}
Snippet B:
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}
(Snippet A can be found at near here, and Snippet B can be found near here)
For Snippet A, I don't think the escape filter is needed because Django's default template engine escapes the string representation of any variable value by default.
For Snippet B, I don't think the safe filter should be used because help_text is no place for any HTML code.
Is my understanding incorrect, or are these two snippets of demo code in Django's documentation problematic the ways I indicated?
Escape-Filter
{% if form.subject.errors %}
<ol>
{% for error in form.subject.errors %}
<li><strong>{{ error|escape }}</strong></li>
{% endfor %}
</ol>
{% endif %}
This snippet from the docs is a bit misleading, I think. As you stated before, yes, usually auto-escaping takes place. The documentation (here) of escape says:
Applying escape to a variable that would normally have auto-escaping applied to the result will only result in one round of escaping being done. So it is safe to use this function even in auto-escaping environments.
This could be useful if you have no control about the parent templates and somewhere in the HTML is {% autoescape off %}. But I think this is a very anusual case.
Safe-Filter
The safe-filter is the opposite. It tells django that no escaping is needed there, if autoescaping is enabled. So because the help text has usually no characters which needs to get escaped, the author of this snippet rates it as safe content and tells django not to use autoescaping - if it is enabled at all.
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}
From my experience, I see no reason why to go without autoescaping and therefore no need for these to filters most of the time.

Django redirect with a custom error message

I'm new in using django templating/frontend and I would like to redirect to a page with a custom message in django.
Here's what I've tried but I can't make It work.
View
return redirect("/login",custom_error="Try Again Later")
Login.html
{% if custom_error %}
<p class="errornote">
{{ custom_error }}
</p>
{% endif %}
Any suggestions on how to make this work?
Please do not encode error messages in the URL. You can use the Django message framework [Django-doc] for this.
Before you make the request, you add a message:
from django.contrib import messages
messages.error('Try again later')
return redirect('name-of-login-view')
It is furthermore better to specify the name of the view, instead of the path, since the path might later change.
In the template(s), probably best in a base.html template that is inherited by (most) other templates, you can then render this with:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}

How to setup Atom Beautify with Twig Files

Hey everyone so I've been loving Atom and specifically the Atom beautify package which automatically cleans up indents and spacing in your markup.
The issue is i can't seem to get it to to work the way i want with Twig even though according to this thread they added support for it. https://github.com/Glavin001/atom-beautify/issues/309
I've commented on that thread asking there but I thought some of you may have found some settings that work well for twig.
It does work to indent the html around the twig pretty well but for example when you do it on something like this
<ul id="navigation">
{% for item in navigation %}
<li>{{ item.caption }}</li>
{% endfor %}
</ul>
it outputs this
<ul id="navigation">
{% for item in navigation %}
<li>{{ item.caption }}</li>
{% endfor %}
</ul>
when what I'm hoping for is
<ul id="navigation">
{% for item in navigation %}
<li>{{ item.caption }}</li>
{% endfor %}
</ul>
Which i could live with the bigger issue is when you have blogs of just twig code like this
{% if entry.description %}
{% set description = entry.description %}
{% else %}
{% set description = entry.body %}
{% endif %}
it puts it all on one line like this
{% if entry.description %}{% set description = entry.description %}{% else %}{% set description = entry.body %}{% endif %}
Im sure theres some setting to fix this but I'm having a hard time finding out what it might be. Anyone else have any luck getting theirs setup properly?
its worth noting that its using Pretty Diff (http://prettydiff.com) for the cleanup and it seems that it does the same with twig code

Django, loop over all form errors

At my template, I want to iterate through all form errors, including the ones that are NOT belonging to a specific field. ( which means for form.errors, it should also display for __all__ errors aswell)
I have tried several versions, Ie:
<div id="msg">
{% if form.errors %}
<div class="error">
<p><span>ERROR</span></p>
<ul>
{% for key,value in form.errors %}
{% for error in value %}
<li>{{ error }}</li>
{% endfor %}
{% endfor %}
</ul>
</div>
{% endif %}
</div>
Still no achievement, I will be greatful for any suggestion.
Form errors in Django are implemented as an ErrorDict instance (which is just a subclass of dict with extras). Try a slight adjustment to your template for loop syntax:
{% for key, value in form.errors.items %}
Are you, by any chance, looking for form.non_field_errors? That is how you would get access to the errors that aren't associated with a particular field.
{% if form.non_field_errors %}
<ul>
{{ form.non_field_errors.as_ul }}
</ul>
{% endif %}
Check the forms.py test suite as well for another example. Search for form.non_field_errors