How to make django-rules work in templates? - django

When using Django-Rules, you can check permissions in views. But mine always returns false. Code in example below always displays 'bar', but must display 'foo':
{% extends 'home/base.html' %}
{% load rules %}
{% has_perm 'anApp.can_access_something' user as can_access_something %}
{% block content %}
{% if can_access_something %}
<p>foo</p>
{% else %}
<p>bar</p>
{% endif %}
...
{% endblock %}
My rules.py looks like this:
#rules.predicate
def testrule(user):
return True
rules.add_perm('anApp.can_access_something', testrule)
So will always return true.

The following line must be in a block:
{% has_perm 'anApp.can_access_something' user as can_access_something %}
This will correctly display 'foo':
{% extends 'home/base.html' %}
{% load rules %}
{% block content %}
{% has_perm 'anApp.can_access_something' user as can_access_something %}
{% if can_access_something %}
<p>something</p>
{% else %}
<p>something else</p>
{% endif %}
...
{% endblock %}

Related

Jinja html code gets incorrectly formatted when saved in VS code

I am writting a Django project using VS Code. I find a strange Jinja formatting issue.
What I want is
{% extends "../base.html" %}
{% load static %}
{% block title %}
{% if category %} {{ category.name }} {% else %} Products {% endif %}
{% endblock %}
{% block content %} {% endblock content %}
But when I save the file or ctrl+s, it gets formatted like
{% extends "../base.html" %} {% load static %} {% block title %} {% if category
%} {{ category.name }} {% else %} Products {% endif %} {% endblock %} {% block
content %} {% endblock content %}
and the render gets error due to broken brackets.
I am using Jinja2 Snippet Kit and Prettier plugins. Do I need to make some changes in setting?
Could someone give me a help? Thank you.

Django: Add class to a div depending on a variable

I'd like to declare in the template a variable that allows me to position an image inside a section on the left or the right. I just don't understand how can I do it on the variable side.
Here's my current code,
main.html :
{% with img_left=False %}
{% include 'section-service.html' %}
{% endwith %}
section-service.html:
<div class="sect_service-inside {% if img_left=True %} serv_img-left {% else %} serv_img-right {% endif %}">
<div id="here_img">...</div>
<div id="here_text">...</div>
</div>
but I get the error Could not parse the remainder: '=True' from 'img_left=True'
Note that I'll use this section multiple times in the main.html page, that's why I'd like to have the variable left-right.
EDIT:
views.py:
def page(request):
return render(request, 'main.html')
I suppose you want widget-tweaks
As example for form:
{% for field in form %}
{% if <something> %}
{% render_field field class="<first class>" %}
{% elif <something other> %}
{% render_field field class="<second class>" %}
{% else %}
{% render_field field class="<other class>" %}
{% endif %}
{% endfor %}

Django admin userlinks is not showing up

I extended base_site.html template and it's working correctly.
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %}
{% trans 'Test Admin' %}
{% endblock %}
{% block userlinks %}
{{ block.super }}
{% endblock %}
{% block nav-global %}{% endblock %}
And now I've created a template for my submit form which looks like this.
{% extends "admin/base_site.html" %}
{% load i18n static %}
{% block userlinks %}
{{ block.super }}
{% endblock %}
But no userlinks is showing up.
If you take a closer look at 'base.html', you will notice that user links are only displayed if has_permission is true.
For my case, I passed has_permission and the user object to my template, and the links appeared.
return render(request, 'account/activate/activate_department_reps.html',
{'department_reps': department_reps, 'has_permission': True, 'user': request.user})

Django: use the forloop value from a parent template

Can I get the forloop value out of a parent template forloop? I.e.:
parent.html
{% extends 'base.html' %}
{% block content %}
{% for i in nnn %}
{% include child.html %}
{% endfor %}
{% end block %}
child.html
{% extends 'base.html' %}
{% block content %}
{{ forloop.counter from parent.html }}
{% endblock %}
def ViewParent(request):
return render_to_response('parent.html', {}, context_instance)
The include template tag supports the passage of arguments using the with keyword.
parent.html:
{% extends 'base.html' %}
{% block content %}
{% for i in nnn %}
{% include child.html with loop_counter=forloop.counter %}
{% endfor %}
{% end block %}
child.html:
{{ loop_counter }}
Note that you probably don't actually mean to extend the child template from the same base template as the parent, so I have omitted that in this example.

How can I get a variable passed into an included template in django

I am a Django newbie and am unable to achieve something trivial. Please help me with this.
I am setting a variable pgurl in my views.py
Am able to access the variable {{pgurl}} in my with_tag.html template. This template includes a pagination.html template into itself
In pagination.html I am unable to use the variable {{pgurl}} and nothing is printed
How can I get this variable passed into the included template?
views.py
def with_tag(request, tag, template_name='main/with_tag.html', current_page=1, pgurl=''):
if request.method == 'GET':
query_tag = Tag.objects.get(name=tag)
primes = TaggedItem.objects.get_by_model(Prime, query_tag)
primes = primes.order_by('-date')
request.page = current_page
tcm_pp = TCM_ITEMS_PER_PAGE
pgurl = request.path
else:
return HttpResponseRedirect(request.path)
return direct_to_template(request, template_name, { 'primes' : primes, 'prime_total' : Prime.objects.count(), 'now': datetime.now(), 'page' : current_page, 'tcm_pp' : tcm_pp, 'tag' : tag, 'pgurl' : pgurl })
with_tag.html
{% extends "base.html" %}
{% load comments %}
{% load pagination_tags %}
...
{% include "pagination.html" %}
{% paginate %}
pagination.html
{% if is_paginated %}
{% load i18n %}
<div class="pagination">
{% if page_obj.has_previous %}
‹‹ {% trans "previous" %}
{% else %}
<span class="disabled prev">‹‹ {% trans "previous" %}</span>
{% endif %}
{% for page in pages %}
{% if page %}
{% ifequal page page_obj.number %}
<span class="current page">{{ page }}</span>
{% else %}
{{ page }}
{% endifequal %}
{% else %}
...
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
{% trans "next" %} ››
{% else %}
<span class="disabled next">{% trans "next" %} ››</span>
{% endif %}
</div>
{% endif %}
It will be helpful if you post the output of the rendered page. The context should get passed, might be your template tags instead. Try to do assert and check to see if the variables were passed correctly.