Django admin userlinks is not showing up - django

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

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.

How to make django-rules work in templates?

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

Is there a way to pass a variable to an 'extended' template in Django?

I want to add some flexibility to my layout template, but I can't find any way to do so.
I'm looking for a way to extend my layout template with variable, i.e. to pass a variable up in the template tree, not down.
# views.py
def my_view_func(request):
return render(request, "child.html")
# child.html
{% extends 'layout.html' with show_sidebar=True sidebar_width_class="width_4" %}
<div>Templates stuff here</div>
# layout.html
{% if show_sidebar %}
<div class="{{ sidebar_width_class }}">
{% block sidebar %}{% endblock %}
</div>
{% endif %}
I have to maintain four templates with a difference in a few lines of code. For example, I have two templates that differ from each other by a sidebar width class. Am I doing something wrong?
I suspect that block is what you are looking for in the first place.
Form your block inside the base template like this:
{% block sidebar_wrapper %}
{% if sidebar %}
<div class="width{{sidebar_width}}">
{% block sidebar %}{% endblock %}
</div>
{% endif %}
{% endblock sidebar_wrapper%}
And on your child template:
{% extends 'layout.html' %}
{% block sidebar_wrapper %}
{% with sidebar=True sidebar_width=4 %}
{{ block.super }}
{% endwith%}
{% endblock sidebar_wrapper%}
What you need is an include template tag. You can include a template in another template and render that with specific context.
{% include 'layout.html' with sidebar=True sidebar_width=4 %}
Check docs here: https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#include
You can achieve this with some technique. I'll show the code then explain below.
# layout.html
{% block content %}
{% if show_sidebar %}
<div class="{{ sidebar_width_class }}">
{% block sidebar %}{% endblock %}
</div>
{% endif %}
{% endblock %}
# child.html
{% extends 'layout.html' %}
{% block content %}
{% with show_sidebar=True sidebar_width_class="width_4" %}
{{ block.super }}
{% endwith %}
{% endblock %}
In layout.html, wrap everything inside {% block content %}
In child.html, {{ block.super }} is like python's super(), which renders everything in the parent template's block. So if you wrap it inside {% with %} tag, all variables that you declare there will be available inside the parent template as well.

Djangocms template not showing up

I have a basic DjangoCMS up and running.
base.html contains:
{% block content %}{% endblock content %}
I also have feature.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block title %}{% page_attribute "page_title" %}{% endblock title %}
{% block content %}
<div>
{% placeholder "feature2" %}
</div>
<div class="jumbotron"">
{% placeholder "feature" %}
</div>
<div>
{% placeholder "content" %}
</div>
{% endblock content %}
I added the "feature2" placeholder in the above, and it correctly displays for editing on the site.
I then added a new line to base.html:
{% block base_logo %}{% endblock base_logo %}
and created a new file, base_logo.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block base_logo %}
<div>
{% placeholder logo %}
</div>
{% endblock base_logo %}
I expected this to also appear on the site for editing, but it doesnt. I have added the base_logo.html to the CMS_TEMPLATES in settings.py and TEMPLATE_DIR is also pointing correctly.
What else do I need to do for Djangocms to pick up my new template?
Take a look at template inheritance.
You're trying to use two {% extends %} tags, which won't work. You should use the {% include %} tag for base_logo, because it seems you'd want to include this in many templates. This question provides more info.

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.