Get Django User group in HTML - html

I am trying to get Django user's group in HTML for an if tag. This is what I tried:
{% ifequal user.groups.all.0 'ABC' %}
{% endif %}
But this is not working. What other way is there?

Try this:
{% for group in request.user.groups.all %}
{% if group.name == 'ABC' %}{% endif %}
{% endfor %}
Or
{% if request.user.groups.all.0.name == 'ABC' %}{% endif %}
You have to access the current user object from the request context variable. For this, make sure that django.template.context_processors.request is in your template settings.
request.user.groups.all.0 returns a Group model object, so you have to compare against the name field.

I think you will have to use a little Python here. For example, a custom template tag:
#register.filter(name='has_group')
def has_group(user, group_name):
return user.groups.filter(name=group_name).exists()
And in your template:
{% if request.user|has_group:"ABC" %}
...
{% endif %}
(Source: http://www.abidibo.net/blog/2014/05/22/check-if-user-belongs-group-django-templates/)
But maybe you should actually use permissions here.
https://docs.djangoproject.com/en/1.8/topics/auth/default/#authentication-data-in-templates
Edit: Here is a more complete example of the custom template tag:
settings.py:
INSTALLED_APPS = [
...
'yourapp',
...
]
Filesystem:
yourproject/
manage.py
yourproject/
settings.py
wsgi.py
...
yourapp/
__init__.py
templatetags/
__init__.py
yourapp_extras.py
...
yourapp_extras.py:
from django import template
register = template.Library()
#register.filter(name='has_group')
def has_group(user, group_name):
return user.groups.filter(name=group_name).exists()
Template:
{% load yourapp_extras %}
{% if request.user|has_group:"ABC" %}
...
{% endif %}
To get a more thorough understanding of this, I highly recommend reading Django's excellent documentation.

<h1>{{ user.groups.all.0 }}</h1>
{% if user.groups.all.0.name == 'Team2' %}
<h1>YES</h1>
{% else %}
<h1>NO</h1>
{% endif %}
Here, the user.groups.all.0 gives you the first group assigned to the user.
For eg. if the logged in user has groups assigned to him as- 'Team2', 'Programmer', 'Beginner'.
Then {{user.groups.all.0}} will print Team2.
I've used it to print and check the logged in user's group in html template.
OR
{% if request.user|has_group:"mygroup" %}
<h1>Has group mygroup</h1>
{% endif %}
Also works fine in django v1.11. This will check if the current user has 'mygroup' assigned to it or not.
However you'll need to add
from django import template
from django.contrib.auth.models import Group
register = template.Library()
#register.filter(name='has_group')
def has_group(user, group_name):
group = Group.objects.get(name=group_name)
return True if group in user.groups.all() else False
inside a group_check.py in below file structure
--app
|templates
|templatetags
|-- group_check.py

Related

Check for a specific url and render jinja2 / html

How can check for a specific URL in jinja2 within html files and then render different html files based on what URL I am currently at?
For eg.
# if {{ request.path == /siteID/ }}
{% include "file1.html" %}
# if {{ request.path == /siteID/abcde/ }}
{% include "file2.html" %}
Current logic I have which I feel is not very good:
<!-- ..... bunch of html lines .... -->
{% if request.path|length > 8 and request.path.startswith('/siteID/') and request.path[8:].strip('/').split('/')|length == 1 %}
{% include "file2.html" %}
{% else %}
{% include "file1.html" %}
{% endif %}
<!-- ..... bunch of html lines .... -->
Also how do I scale this if I want to do something in the future like:
# if {{ request.path == /siteID/abcde/uvwxyz }}
{% include "file3.html" %}
Since you want to scale this later, don't do this in the template. Put the logic into a view function, then pass a variable to the render function that stores the name of the template you want to include. E.g.:
def the_view(request):
if request.path == '/siteID/abcde/':
template = 'file1.html'
elif request.path == '/siteID/fgh/':
template = 'file2.html'
else:
template = 'file_default.html'
return render(request, 'base.html', {'template_to_include': template})
Then in your base.html template put this dynamic import line somewhere:
{% include template_to_include %}

How can I use get query in jinja while doing a django project?

So I am doing a django project. I have sent a dictionary(which holds a foreign key) to my html template. It has 'name'(which is a foreignkey),'bills','flat' keys. So when I am trying to print the name of a user it is actually printing the id of the user. But I want the name. I have used users = User.objects.all().values('name','bills','flat') and then passed the users in the template. The I tried to print like this:
{% for info in users %} {{info}} {% endfor %}
But instead of giving the name it is giving me the id number of the user.
My models.py file:
`
class Owner(models.Model):
name = models.CharField(max_length=100)
email = models.CharField(max_length=100)
class Payment(models.Model):
name=models.ForeignKey(Owner,on_delete=models.CASCADE,null=True)
flat = models.CharField(max_length=1,null=True,blank=True)
floor = models.IntegerField(default=0)`
My views.py file:
`
info = Payment.objects.all()
dict = {'1':[info.values('name','flat','floor','water_bill')]}
`
My Template file:
`
{% for j in dict.0 %}
<tr>
{% for i in j.values %}
<td>{{i}}</td>
{% endfor %}
</tr>
{% endfor %}
`
The return statement should look something like this,
return render_template(file.html, dict = dict)
Then you can use the dict variable in jinja as follows,
{% for j,k in dict %}
<tr>
{% for i in j.values %}
<td>{{i}}</td>
{% endfor %}
</tr>
{% endfor %}

How to add a new property to Jekylls posts?

I want to do something like:
{% for post in site.posts %}
{% assign post.myProprty = "something" %}
{% endfor %}
But it doesn't work. Is that another way to do it?
I could make it happen using a plugin.
plugin code:
module Jekyll
module AddProp
def addProp(input, key, value)
input[key] = value
return input
end
end
end
Liquid::Template.register_filter(Jekyll::AddProp)
Usage:
{% for post in site.posts %}
{% assign post = post | addProp: "myProprty", "something" %}
{% endfor %}
Plugin GitHub repository

How do I display a message in Jinja (flask) if there is no object data to display

I have an object in python views.py that references the events database table. When there is data, it displays the data in my html template, however, when there is no data, I cannot figure out the {% if %} function that would display the message "No data found."
I have tried Tadeck's post, but the is defined always seems to evaluate to true even if there is no data to display. Thank you for your help.
{% if events is defined %}
value of variable: {{ events }}
{% else %}
variable is not defined
{% endif %}
views.py
events = db.session.query(Eventdetails, Buyers).\
join(Buyers).\
filter(Eventdetails.events_id == event_id)
return render_template(
self.template_file, events=events, the_event=the_event,
event_id=event_id
)
You are passing events as the query. You want it to be the query results:
events = db.session.query(Eventdetails, Buyers).\
join(Buyers).\
filter(Eventdetails.events_id == event_id).all()
Things to try:
{% if events %}
{% if events|length > 0 %}
{% if events != [] %}
This will help you. When event have data then it will go inside the if condition otherwise else will be execute No data found.
{% if events %}
value of variable: {{ events }}
{% else %}
No data found.
{% endif %}

Whare are the difference between set and with in jinja

I am beginner in computer science and trying to use python flask framework to create a webserver.
In the tutorials, I have seen the below jinja usage in the HTML which are
{% set a = somefunction() %}
{% with a = somefunction() %}
May I know what are the differences? Thanks.
While you may use {% set %} to define a variable, the {% with %} statement additionally creates a new scope which can be ended using the {% endwith %} statement.
For example:
{% with myvar=1 %}
...
{% endwith %}
myvar will only be available before the corresponding endwith.
You can also use with to create a local scope:
{% with %}
{% set myvar=1 %}
...
{% endwith %}
myvar will only be available within the given scope.
You can find information about these statements here (the examples shown here were taken from there also).
Variables inside a {% with %} statement are restricted to that particular statement, while variables created via {% set %} are accessible from anywhere in the template (they are global).
>>> import jinja2
>>> t = jinja2.Template("{% set a = 'Global' %}{% with a = 'Local' %}{{ 'First a is ' + a +'\n' }}{% endwith %}{{ 'Second a is ' + a }}")
>>> print(t.render())
First a is Local
Second a is Global