clients_list
{'clients': [
{'id': 357995, 'value': 1.0},
{'id': 369743, 'value': 0.9}
]}
{% try %}
{% if clients_list %}
{% for client in clients_list %}
{% for user in client %}
{% raw user.id %}
{% raw user.value %}
{% end %}
{% end %}
{% end %}
{% except %}
{% end %}
Output expected:
357995
1.0
369743
0.9
The problem is that loop in template is wrong. How can i access the id and value?
This is a tornado template, but i think that is similar to django.
Update:
{% try %}
{% if clients_list %}
{% for client in clients_list %}
{% raw client %} // outputs the clients_list
{% for user in client %}
{% raw user %} outputs 'clients'
{% end %}
{% end %}
{% end %}
{% except %}
{% end %}
Here is the solution.
{% try %}
{% if clients_list %}
{% for client in clients_list %}
{% for user in client['clients'] %}
{% raw user['id'] %}
{% raw user['value'] %}
{% end %}
{% end %}
{% end %}
{% except %}
{% end %}
Related
I am having a problem and need your help.
I am making a django website, following information need to be verified. If it pass registration will be success and fail notice will show up. But Mean while there are no problem with success, but Notice won't show up if fail. Here are my form.py, could anyone help me? Much Thanks.
if settings.VERIFY_BY_SFID and all([i in cleaned_data for i in fields]):
number = cleaned_data["mobile_number"]
verified =SignUpFailureAttemptService.verify_mobile_number(number)
if not verified:
self.add_error(None, _("Your application was rejected."))
Template
{% extends 'accounts/login_base.html' %} {% load i18n %} {% load staticfiles %} {% load semanticui %} {% load common_templatetags %} {% block welcome_text %} {% trans 'Create your personal
E-wallet account.' %} {% endblock %} {% block slogan_text %} {% trans 'Buy and sell with protection
Easy and secure to send and receive money' %} {% endblock %} {% block bigger_text %} {% blocktrans %}Already have an account?{% endblocktrans %} {% trans 'Sign in' %} {% endblock %} {% block left_content %}
STEPS:
{% include 'accounts/signup/steps.html' with active_step=step steps=5 %}
{% block form_title %} {{ form_title }} {% endblock %}
{% if messages %} {% for message in messages %}
{{ message }}
{% endfor %} {% endif %}
{% block form_block %}
{% csrf_token %} {{ form.non_field_errors }} {% for hidden_field in form.hidden_fields %} {{ hidden_field.errors }} {{ hidden_field }} {% endfor %} {% for field in form.visible_fields %} {% if field.name == 'date_of_birth' %}
{{ field.label }}*
{% endif %}
{% if field.name == 'tos' %} {% blocktrans %} I agree to {% endblocktrans %} PDS and FSG {{ field }}
{% else %} {{ field }} {% endif %}
{{ field.help_text }}
{{ field.errors }}
{% endfor %}
{% if prev_step %} « {% trans 'Back' %} {% endif %}
{% if next_step_title %}
{% trans 'NEXT STEP:' %} {{ next_step_title }}
{% endif %}
{% endblock %} {% endblock %} {% block scripts_end %} {% endblock %}
I am using the following code to access the first message in Django template,
{% if messages %}
{% for message in messages %}
{% if forloop.first %}
{{ message }}
{% endif %}
{% endfor %}
{% endif %}
How I can achieve the same without using the for loop in a single statement.
Following should work:
{% if messages %}
{{ messages.0 }}
{% endif %}
I would like to have different content for users and guests in my home page's template using Django 1.9 and Django CMS 3.3.1.
It could be acomplished by making subpages and showing the corresponding content in the ancestor based on authentication conditional, but that makes the page structure overly complicated.
Is there an easy way of adding these placeholders straight to the template?
I have tried this:
{% extends "base.html" %}
{% load cms_tags %}
{% block title %}{% page_attribute "page_title" %}{% endblock title %}
{% block content %}
{% if not user.is_authenticated %}
{% placeholder "guests" %}
{% endif %}
{% if user.is_authenticated %}
{% placeholder "authenticated" %}
{% endif %}
{% placeholder "content" %}
{% endblock content %}
But as I am authenticated when I'm editing the content, I cannot access the guests placeholder.
Try this:
{% block content %}
{% if request.toolbar.build_mode or request.toolbar.edit_mode %}
{% placeholder "guests" %}
{% placeholder "authenticated" %}
{% else %}
{% if not user.is_authenticated %}
{% placeholder "guests" %}
{% endif %}
{% if user.is_authenticated %}
{% placeholder "authenticated" %}
{% endif %}
{% endif %}
{% placeholder "content" %}
{% endblock content %}
I have some experience with Django CMS but don't know if this will work. The idea is to check if we're in edit mode by inspecting corresponding request variables. See this answer.
Update by #V-Kopio:
The answer given above works fine in practice but Django warns about dublicate placeholders. This can be avoided by combining the if and else blocks:
{% block content %}
{% if not user.is_authenticated or request.toolbar.build_mode or request.toolbar.edit_mode %}
{% placeholder "guests" %}
{% endif %}
{% if user.is_authenticated %}
{% placeholder "authenticated" %}
{% endif %}
{% placeholder "content" %}
{% endblock content %}
{% if not User_Tld_Entered %}
#HTML HERE
{% endif %}
{% if User_No_Auth_Tld > 0 %}
{% for NotAuthDomain in User_No_Auth_Tld %}
#HTML HERE
{% endfor %}
{% endif %}
{% else %}
{% for tld in tld_set %}
#HTML HERE
{% endfor %}
{% endif %}
For some reason it is failing on the else condition above throwing:
Invalid block tag: 'else', expected 'endblock'
What am I doing wrong here?
Thank you.
The first {% endif %} ends the first block. You then have if User_No_Auth_Tld > 0 and you close that block too. By the time {% else %} comes up, you're no longer within an if block so it's an unexpected tag.
If you remove the first {% endif %} it will work
{% if not User_Tld_Entered %}
#HTML HERE
{% endif %} <- this one needs to go
{% if User_No_Auth_Tld > 0 %}
{% for NotAuthDomain in User_No_Auth_Tld %}
#HTML HERE
{% endfor %}
{% endif %} <- or this one nedds to go
{% else %} <-- whos else is this
{% for tld in tld_set %}
#HTML HERE
{% endfor %}
{% endif %}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Django templates syntax error
When i am using {% if request.user.is_authenticated %} condition for redirection on this code it throws error Invalid block tag: 'else'
{% if request.user.is_authenticated %}
{% extends "pages/page.html" %}
{% load mezzanine_tags shop_tags i18n %}
{% block body_id %}category{% endblock %}
{% block main %}{{ block.super }}
{% regroup products by category as products_by_category %}
{% for c in products_by_category %}
......
{% for p in c.list %}
......
{% if p.num_in_stock == None %}
...
{% else %}
{% if p.num_in_stock < 4 %}
...
{% endif %}
{% endif %}
.....
{% endfor %}
......
{% endfor %}
{% endblock %}
{% else %}
<script>
window.location="/stylequiz/";
</script>
If i am using this script then it gives no error
{% if request.user.is_authenticated %}
<h1>welcome</h1>
{% else %}
<script>
window.location="/stylequiz/";
</script>
{% endif %}
I think there must be a problem with nested if .
You can't put {%extends%} tag inside {%if%}. It should be first tag in the template.
From django docs Template inheritance
If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won't work, otherwise.