Using Django filters as variables - django

I am wondering if we could use django filter as a variable where a python method formatting text can set the filter value which can be rendered by the template
The usual way filter works is like this
{{ html_text | safe }} or {{ plain_text | linebreaks }}
I want to set the values for filters 'safe'/'linebreaks' in a variable and render it like this
{{ text | filter_variable }}
Researched online, but couldn't figure it out, any help would be appreciated.

I second what Chris Pratt said, but I'd do it like this:
from django.template import defaultfilters
#register.filter
def apply_filter(value, filter):
return getattr(defaultfilters, filter)(value)
that way it works for any filter.

You can't do that, but you can create a custom filter that accepts a parameter:
from django.template import defaultfilters
#register.filter
def apply_filter(value, filter):
if filter == 'safe':
return defaultfilters.safe(value)
if filter == 'linebreaks':
return defaultfilters.linebreaks(value)
else:
# You might want to raise an exception here, but for example purposes
# I'll just return the value unmodified
return value
Then, in your template:
{{ text|apply_filter:filter_variable }}

This works if you have a few filters:
{% ifequal filter_variable 1 %}{{ text|safe }}{% endifequal %}
{% ifequal filter_variable 2 %}{{ text|linebreaks }}{% endifequal %}
{% ifequal filter_variable 3 %}{{ text|yourfilter }}{% endifequal %}

Related

Boolean check inside a for loop in a Django template

In a Django template I have the following for loop
{% for document in documents %}
<li>{{ document.docfile.name }}</li>
{% endfor %}
Through this loop I am showing the user all the uploaded files of my app.
Now say that I want to show the user only the files he/she has uploaded.
I have the current user in the variable {{ request.user }}
and also I have the user who did the i-th upload in {{ document.who_upload }}
My question is how can I compare these two variables inside the loop to show only the uploads that have a who_upload field that of the current user?
For example I tried the syntax
{% if {{ request.user }} == {{ document.who_upload }} %}
{% endif %}
but it does not seem to work.
What is the proper syntax for this check?
Thank you !
This should get the job done:
{% if request.user.username == document.who_upload.username %}
{% endif %}
But you should consider performing this logic in your view. This is assuming you're not looping over the entire queryset anywhere else.
views.py
========
from django.shortcuts import render
from .models import Document
def documents(request):
queryset = Document.objects.filter(who_upload=request.user)
return render(request, 'document_list.html', {
'documents': queryset
})
A better option would be to compare the users' primary keys, instead of comparing user objects, which most definitely will be different.
{% if request.user.pk == document.who_upload.pk %}
<span>You uploaded this file</span>
{% endif %}

Django Custom Tag Conditional Logic

In this project I'm working with (I'm very new to Django), there are custom tags i.e. {{ custom_tag }} that a previous developer created.
In the HTML file, I find myself doing the following block of conditional logic many times in the same HTML file.
{% if custom_tag == "Blog Tag" %}
Blog
{% elif custom_tag == "About Tag" %}
About
{% else %}
etc...
{% endif %}
Are there ways that I can replace all of that conditional logic into something like {{ custom_tag|pretty }} or {{ pretty_custom_tag }}?
You can write your own criting a custom filter which would let you use {{ custom_tag|tag_pretty }}: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
For example:
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
#register.filter
#stringfilter
def pretty_tag(value):
return value.rpartition(" ")[0]

Accessing initial value of django forms when iterating over the fields

I'm trying to do something pretty simple; I'd like to apply a "hidden" style to a form field inside a django template when I've passed in some initial value like this:
form = form_class(initial={'field':data})
Normally, it would be like this:
<li class="{{form.somefield.name}} {% if form.somefield.initial %} hidden{% endif %}>
...
</li>
But I'm iterating over the forms, so what I want do do is something that looks like this:
{% for field in form %}
<li class="{{field.name}} {% if field.initial %} hidden{% endif %}">
...
</li>
{% endfor %}
but this doesn't work, because field.initial only has the value defined as initial to the field in the form, not the data that's passed in at the form's creation. Is there a good solution for this besides just breaking out the iterating into individual forms?
Some (bad) solutions I've thought of:
overriding init to stuff values form self.initial into self.fields;
writing a template tags called {% hideifhasinitial %}
adding a method to the form that uses zip on self and self.initial (doesn't work, since self.initial only had one element and self had 4, it only iterated over 1 element, and the keys (field names) didn't match up).
how about this?
{% for field in form %}
{% if field.name in field.form.initial.keys %}
...
{% endif %}
{% endfor %}
Initial data can be accessed on the value attribute, initial data represents the value of the field:
{{field.value}}
Turns out there's a way easier way to do this.
{% if field.name in form.initial.keys %}
The solution with the initial keys has not worked for me, because the field contains as a value an empty string. I had to write my own custom tag:
from django import template
register = template.Library()
#register.simple_tag
def field_empty(field):
if not field.form.initial.get(field.name):
return ' hidden'
return ''
In your example, I would use the tag this way:
<li class="{{ field.name }} {% field_empty field %}">

Django templates: value of dictionary key with a space in it

In a Django template, is there a way to get a value from a key that has a space in it?
Eg, if I have a dict like:
{"Restaurant Name": Foo}
How can I reference that value in my template? Pseudo-syntax might be:
{{ entry['Restaurant Name'] }}
There is no clean way to do this with the built-in tags. Trying to do something like:
{{ a.'Restaurant Name'}} or {{ a.Restaurant Name }}
will throw a parse error.
You could do a for loop through the dictionary (but it's ugly/inefficient):
{% for k, v in your_dict_passed_into_context %}
{% ifequal k "Restaurant Name" %}
{{ v }}
{% endifequal %}
{% endfor %}
A custom tag would probably be cleaner:
from django import template
register = template.Library()
#register.simple_tag
def dictKeyLookup(the_dict, key):
# Try to fetch from the dict, and if it's not found return an empty string.
return the_dict.get(key, '')
and use it in the template like so:
{% dictKeyLookup your_dict_passed_into_context "Restaurant Name" %}
Or maybe try to restructure your dict to have "easier to work with" keys.
You can use a custom filter as well.
from django import template
register = template.Library()
#register.filter
def get(mapping, key):
return mapping.get(key, '')
and within the template
{{ entry|get:"Restaurant Name" }}

How to represent "{{" in a django template?

I'm trying to output in bibtex format in Django and the template looks like this:
#{{ pubentry.type }{,
author = {{% for author in pubentry.authors.all %}{{ author.first_name }} {{ author.middle_name }} {{ author.last_name }}{% if not forloop.last %} and {% endif %}
{% endfor %}},
title = {{{ pubentry.title }}},
journal = {{{ pubentry.journal }}}
}
The problem is with the {{{ or {{%. One way to go around the problem is to add a space after the first {, but that kind of tamper the format. What's the right way to escape { in Django templates?
Have a look at the templatetag tag:
Output one of the syntax characters used to compose template tags.
Since the template system has no concept of "escaping", to display one of the bits used in template tags, you must use the {% templatetag %} tag.
What you are after is:
{% templatetag openvariable %}
Maybe there is a nicer solution because this does not increase readability...
Another (more flexible) approach may be to convert the values to a bibtex-like value before sending them to your template. You'll probably need to do this anyway to escape some of the characters that bibtex/latex can't handle. Here's something similar I prepared earlier:
import datetime
class BibTeXString(unicode):
pass
def bibtex_repr(obj):
""" A version of the string repr method, that always outputs variables suitable for BibTeX. """
# If this has already been processed, it's ok
if isinstance(obj, BibTeXString):
return obj
# Translate strings
if isinstance(obj, basestring):
value = unicode(obj).translate(CHAR_ESCAPES).strip()
return BibTeXString('{%s}' % value)
# Dates
elif isinstance(obj, datetime.date):
return BibTeXString('{%02d-%02d-%02d}' % (obj.year, obj.month, obj.day))
# Integers
if isinstance(obj, (int, long)):
return BibTeXString(str(obj))
else:
return BibTeXString(repr(obj))
CHAR_ESCAPES = {
ord(u'$'): u'\\$',
ord(u'&'): u'\\&',
ord(u'%'): u'\\%',
ord(u'#'): u'\\#',
ord(u'_'): u'\\_',
ord(u'\u2018'): u'`',
ord(u'\u2019'): u"'",
ord(u'\u201c'): u"``",
ord(u'\u201d'): u"''" ,
ord(u'\u2014'): u'---',
ord(u'\u2013'): u'--',
}
You could even use this as a template filter if you wanted, making your template look like:
#{{ pubentry.type }{,
author = {% filter bibtex %}{% for author in pubentry.authors.all %}{{ author.first_name }} {{ author.middle_name }} {{ author.last_name }}{% if not forloop.last %} and {% endif %}{% endfor %}}{% endfilter %},
title = {{ pubentry.title|bibtex }},
journal = {{ pubentry.journal|bibtex }}
}
But I would escape the content before it gets to the template, so that your template just needs to do this:
#{{ pubentry.type }{,
{% for field in fields %}{{ field }}{% if not forloop.last %},{% endif %}{% endfor %}
}
Or even leave out the template altogether at this stage. Good luck!
With the templatetag template tag.
title = {% templatetag openvariable %}{% templatetag openbrace %} pubentry.title {% templatetag closevariable %}{% templatetag closebrace %},