Django: List of symbols for templates - django

Not really a direct coding question, but still something I wasn't able to find on my own ... and related to coding ;)
Is there any list which symbols/placeholders are available in a Django template? So e.g. {{user}} etc.

There is the list of built-in filters and tags, in addition the default template context processors add the following:
{{ user }} and {{ perms }} from django.contrib.auth.context_processors.auth
{{ debug }} and {{ sql_queries }} from django.core.context_processors.debug
{{ LANGUAGES }} and {{ LANGUAGE_CODE }} from django.core.context_processors.i18n
{{ MEDIA_URL }} from django.core.context_processors.media
{{ STATIC_URL }} from django.core.context_processors.static
{{ csrf_token }} from django.core.context_processors.csrf
{{ request }} from django.core.context_processors.request
{{ messages }} from django.contrib.messages.context_processors.messages

A list of built in tags and filters?
https://docs.djangoproject.com/en/1.6/ref/templates/builtins/
EDIT: still not sure what you mean by symbols. {{user}} is not a symbol, it is a template variable.
Perhaps you could refer to the code itself: https://github.com/django/django/blob/master/django/core/context_processors.py

There are tools like django-debug-toolbar and django-template-debug which provide this functionality for debugging purposes.

Related

Is it ok to have two or more Django forms in one HTML form?

I know it's possible to have more than one Django form in one HTML form, even formsets, and standard forms combined and it works perfectly fine. I want to know if it is a good practice and how this can impact security if it will at all? I couldn't find any information about those cases but in tutorials, I saw both (multiple Django forms in one HTML and every Django form in a separate HTML form) in some cases is necessary since the two forms can do different things like update and delete for example. In my case, all forms POST data to different models, and then all are combined in one model. Please take a look at the example (This is a simple example and not the actual code) below:
<form action="" method="POST">
{% csrf_token %}
{{ form.field1 }}
{{ form.field2 }}
{{ form.field3 }}
{% for field in dates_form %}
{{ field.Loading_date_from }}{{ field.Loading_time_from }}{{ field.Loading_date_to }}{{ field.Loading_time_to }}
{% endfor %}
{% for field in dates_form2 %}
{{ field.Loading_date_from }}{{ field.Loading_time_from }}{{ field.Loading_date_to }}{{ field.Loading_time_to }}
{% endfor %}
{{ form.field4 }}
{{ form.field5 }}
<input type="submit">
</form>
Yes, it is okay, as long as you handle them properly in the view. There’s a package to help with this, including support for model forms as well:
https://github.com/kennethlove/django-shapeshifter
Full disclosure, I’m a contributor to the package.

Using multiple filters for django text? [duplicate]

For me this works:
{{ game.description|safe }}
But this fails:
{{ game.description|safe|slice:"65" }}
Is there a way to apply two or more filters on a variable in Django templates?
Although it's quite past when the OP posted the question, but for other people that may need the info, this seems to work well for me:
You can rewrite
{{ game.description|safe|slice:"65" }}
as
{% with description=game.description|safe %}
{{description|slice:"65"}}
{% endwith %}
Is description an array or a string?
If it is a string, you might want to try truncatewords (or truncatewords_html if the description can contain HTML),
{{ game.description|safe|truncatewords:65 }}
Reference: Built-in filter reference, truncatewords.
(I'm new to Django so my apologies if slice works on strings.)
change
{{ game.description|safe|slice:"65" }}
to
{{ game.description|safe|slice:":65" }}
you are missing the colon
This may work:
{% filter force_escape|lower %}
This text will be HTML-escaped, and will appear in all lowercase.
{% endfilter %}
Reference: Built-in tag reference, filter.

Multiple lines concatenating into a single line in VS code for flask templates

I'm using VS Code for web development and found this behavior annoying :
What I wanted to be in flask-templates is below
{{ render_field(form.email, placeholder=form.email.label.text) }}
{{ render_field(form.password, placeholder=form.password.label.text) }}
{{ render_field(form.textarea) }}
{{ render_radio_fields(form.radios) }}
{{ render_field(form.selects) }}
But when I save this html file they are joining into a single line like below !
{{ render_field(form.email, placeholder=form.email.label.text) }} {{ render_field(form.password, placeholder=form.password.label.text) }} {{ render_field(form.textarea) }} {{ render_radio_fields(form.radios) }} {{ render_field(form.selects) }}
Any help with this ? or Any settings need to be changed.
I cannot found any extensions in VS Code that support this.
Thanks

print out django request.META in a template

I am trying to print out the values inside the request.META in a template but I cannot get it right. All I got is an error Could not parse the remainder: '[i]' from 'REQ_META[i]'
below is my code:
in my views.py
def index (request):
template = loader.get_template('app/index.html')
page_data = { 'REQ_META': request.META}
context = RequestContext(request, page_data)
return HttpResponse(template.render(context))
in index.html
{% for i in REQ_META %}
{{ i }} = {{ REQ_META[i] }} <br />
{% endfor %}
Well, the right way to inspect the request.META objects would be using pdb in the view, or using tools like django-debugtoolbar.
In my opinion, django debug toolbar is an extremely handy tools for debugging purposes.
Regardless, Your issue is, REQ_META is a dictionary, and the way to parse the elements of the dictionary is:
{% for k, v in REQ_META %}
{{ k }} = {{ v }} <br />
{% endfor %}
Documentation here
There is already an answer, but thought it could be useful for future use:
You just need to access the object like this {{ REQ_META.i }} instead of {{ REQ_META[i] }}
Another option is to use django pprint template filter
{{ REQ_META|pprint }}
Which will always print nicely dict objects (and any other python object)

Using increased/decreased variables in django templates

Maybe it's a little bit stupid question, but I didn't find an answer. Is there any way to use increased/decreased variables in django templates?
e.g.{{ some_variable + 1 }}
There's a built-in add filter:
{{ some_variable|add:"1" }}
One way of doing this is by using a django template filter.
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters
def inc(value):
return value+1
and then:
{{ some_variable|inc }}
Inside for loop use forloop.counter that will automatically increase the counter till the records.
{% for a in object_list %}
{{ forloop.counter }}
{% endfor %}