If not true (!true) - if-statement

In golang's template/html package, I can use {{ if .loggedIn }} to check if logged in is true.
How do I check if .loggedIn is false without using ne or eq?
For example, I am looking for something like
{{ if !.loggedIn }}
<h1>Not logged in</h1>
{{ end }}

Use the function not:
{{ if not .loggedIn }}
<h1>Not logged in</h1>
{{ end }}

Related

How to set a variable value in a Flask HTML template

I am trying to a assign a value to a WTForm field within the HTML template. I need to do it this way as the value assigned will depend on what iteration of the for loop it is in. Any other way of passing a value back to the python script will work as well.
I am currently getting an error. I am trying to assign the record field of the form equal to the id of the record it is currently viewing.
Below is the code:
<div class="modal-body">
<p>Name of Training: {{ record.training_name }}</p>
<p>Name of Provider: {{ record.training_provider }}</p>
<p>Date of Training: {{ record.date_submitted.strftime('%d/%m/%Y') }}</p>
<p>CPD Hours: {{ record.cpd }}</p>
<p>Certificate: View</p>
<form action="" method="post">
{{ form.hidden_tag() }}
<fieldset>
<div class="form-group">
{{ form.reason(class="form-control", placeholder="Reason for Denial (if applicable)") }}
{% set form.record.data=record.id %}
</div>
</fieldset>
When attempting to use the solution above I am getting the following error: jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got '.'
If I'm understanding you correctly:
{{ form.record(value=record.id, class="d-none") }}
instead of
{% set form.record.data=record.id %}

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

Django: List of symbols for templates

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.

fetch variables in django templates

I am new to django .
I am facing a small problem but unable to solve it.
In my template when I try to access any variable using {{ }} but it returns blank while displaying something as detailview.whereas it works fine in listview.
CODE:urls.py
urlpatterns = patterns('',
url(r'^$', ListView.as_view(
queryset=Profile.objects.all().order_by("First_Name"),
template_name="STUDENT_REGISTRATION.html")),
url(r'^(?P<pk>\d+)/$',DetailView.as_view(
model=Profile,
template_name="Profile.html")),
TEMPLATE:
{%extends "base.html" %}
{% block content %}
<h2>Registration No. :- {{ Profile.Registration_No }} <br>
Full Name : {{ Profile.First_Name }} {{ Profile.Last_Name }} </h2>
<div class="Profile_meta">
{{ Profile.Date_of_Birth}}
</div>
<div class="Profile_body">
{{ Profile.Permanent_Address|safe|linebreaks }}
</div>
{%endblock%}
Plz help..
You should refer to your Profile instance as:
{{ object.First_Name }} {{ object.Last_Name }}
By the way, take a look at PEP8, try to keep CamelCaseNames for class names and underscores_names for instances. It will make your code easier to read.

Django template vars and list indices

I have a strange problem.
I use paginator to display some data in each page, however, I do not see any possibility to add other attributes I need to the model, so I have created another list of dicts with additional data.
In template:
{% for x in p.object_list %}
{% with c=forloop.counter0 %}
{{ info.c }} <!-- prints nothing, while {{ info }} prints all the list of dicts and {{ c }} prints 0, for example. {{ info.0 }} prints everything as intended too. -->
{% endwith %}
{% endfor %}
{{ info.c }} accesses info['c'], info.c, etc. You want the slice filter instead.