issue with nested if? [duplicate] - python

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.

Related

How to extend multiple bases or conditional {% extends %} in one html page in django?

{% if request.user.profile.emp_desi in qa_list %}
{% extends "qa_base.html" %}
{% elif request.user.profile.emp_desi in mgr_list %}
{% extends "manager_base.html" %}
{% else %}
{% extends "common_base.html" %}
{% endif %}
How can I solve this problem??
based on designation I want to extend different different bases.
You can use include
You need to create a directory and template layout in such a way that
you will have one base and multiple includes.
https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#include
{% if request.user.profile.emp_desi in qa_list %}
{% include "qa_base.html" %}
{% elif request.user.profile.emp_desi in mgr_list %}
{% include "manager_base.html" %}
{% else %}
{% include "common_base.html" %}
{% endif %}

Access parent for loop scope in child template in Jinja2

parent.txt
{% for dict in list_of_dictionaries %}
{% block pick_dictionary_element %}
{% endblock %}
{% endfor %}
child_one.txt
{% extends "parent.txt" %}
{% block pick_dictionary_element %}
{{ dict.a }}
{% endblock %}
child_two.txt
{% extends "parent.txt" %}
{% block pick_dictionary_element %}
{{ dict.b }}
{% endblock %}
Then:
from jinja2 import Template, Environment, FileSystemLoader
e = Environment(loader=FileSystemLoader("./"))
e.get_template("child_one.txt").render(list_of_dictionaries=[{'a': 'a', 'b': 'b'}])
produces an empty output. How can I access the dict var from the parent for loop? I kind of imagined jinja just in-lining the pick_dictionary_element and the child having the for loop scope of its parent?
The key to what you are trying to do is to use the scoped keyword on your block:
{# parent.txt #}
{% for dict in list_of_dictionaries %}
{% block pick_dictionary_element scoped %}
{% endblock %}
{% endfor %}
Why was this hard to debug?
You made the mistake of using the name dict in the loop:
{% for dict in list_of_dictionaries %}
The side effect of this was that the child template did not readily complain, since the symbol dict exists in its context. If instead, you had done something like:
{# parent.txt #}
{% for a_dict in list_of_dictionaries %}
{% block pick_dictionary_element %}
{% endblock %}
{% endfor %}
{# child_one.txt #}
{% extends "parent.txt" %}
{% block pick_dictionary_element %}
{{ a_dict.a }}
{% endblock %}
You would have been told:
jinja2.exceptions.UndefinedError: 'a_dict' is undefined
Starting with Jinja 2.2, you can explicitly specify that variables are available in a block by setting the block to “scoped” by adding the scoped modifier to a block declaration
http://jinja.pocoo.org/docs/2.9/templates/#block-nesting-and-scope
{# parent.txt #}
{% for dict in list_of_dictionaries %}
{% block pick_dictionary_element scoped %}
{% endblock %}
{% endfor %}

declare variable in if else block in django framework

I am doing a conditional block in loop django template , but unable to find exact answer for this.
Please consider my code here,
{% for data in app_data %}
{% if forloop.counter == 1 %}
{% declare_some_variable = 'hello' %}
{% else %}
{% declare_some_variable = 'bye' %}
{% endif %}
{{ declare_some_variable }} {{ data.name }}
{% endfor %}
This is what I want. But it does not work.
Try to use the with tag.
{% with declare_some_variable = "hello" %}
{% endwith %}
You can read more about with here
Or you can simply do:
{% trans "hello" as declare_some_variable %}

Django CMS – Show different content for users and guests in same template

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 %}

Querying for specific articles (via tag/category) in Pelican themes

Is it possible to set query parameters via jinja in Pelican template files?
index.html
{% if articles %}
{% for article in articles_page.object_list if article.category == 'article' %}
#stuff
{% endfor %}
{% endif %}
This will return articles in the article category, but only if they happen to be in the articles already queried for. The desirable setup would be to grab x articles in y category (or with y tag) - is that possible?
This code snippet works for me to bring back a list of all articles matching a tag:
{% block content %}
<ul>
{% for article in articles if FAVORITES_TAG in article.tags %}
{% if loop.index <= FAVORITES_COUNT %}
<li>{{ article.title }}</li>
{% endif %}
{% endfor %}
</ul>
{% endblock %}
I set the FAVORITES_TAG and FAVORITES_COUNT variables in the config. I hope that helps.
I ran into the same problem, and found a solution
{% if articles %}
{% for article in articles_page.object_list if article.category.name == 'article' %}
#stuff
{% endfor %}
{% endif %}

Categories

Resources