113👍
You need custom template tag:
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()
In your template:
{% if request.user|has_group:"mygroup" %}
<p>User belongs to my group
{% else %}
<p>User doesn't belong to mygroup</p>
{% endif %}
Source: http://www.abidibo.net/blog/2014/05/22/check-if-user-belongs-group-django-templates/
Docs: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
35👍
In your app create a folder ‘templatetags’. In this folder create two files:
__init__.py
auth_extras.py
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
It should look like this now:
app/
__init__.py
models.py
templatetags/
__init__.py
auth_extras.py
views.py
After adding the templatetags module, you will need to restart your server before you can use the tags or filters in templates.
In your base.html (template) use the following:
{% load auth_extras %}
and to check if the user is in group “moderator”:
{% if request.user|has_group:"moderator" %}
<p>moderator</p>
{% endif %}
Documentation: https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/
- [Django]-How to add custom search box in Django-admin?
- [Django]-Django multiprocessing and database connections
- [Django]-Django set default form values
18👍
I’d say that the best way is:
yourapp/templatetags/templatetagname.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()
yourapp/templates/yourapp/yourtemplate.html:
{% load has_group %}
{% if request.user|has_group:"mygroup" %}
<p>User belongs to my group</p>
{% else %}
<p>User does not belong to my group</p>
{% endif %}
EDIT: added line with template tag loading as was advised in comments.
EDIT2: fixed minor typo.
- [Django]-VueJS + Django Channels
- [Django]-What's the difference between select_related and prefetch_related in Django ORM?
- [Django]-Django DRF with oAuth2 using DOT (django-oauth-toolkit)
13👍
Watch out that you’ll get an exception if the group does not exist in the DB.
The custom template tag should be:
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):
try:
group = Group.objects.get(name=group_name)
except Group.DoesNotExist:
return False
return group in user.groups.all()
Your template:
{% if request.user|has_group:"mygroup" %}
<p>User belongs to my group
{% else %}
<p>User doesn't belong to mygroup</p>
{% endif %}
- [Django]-How to completely dump the data for Django-CMS
- [Django]-Django values_list vs values
- [Django]-Open the file in universal-newline mode using the CSV Django module
10👍
In your template
{% ifequal user.groups.all.0.name "user" %}
This is User
{% endifequal %}
- [Django]-POST jQuery array to Django
- [Django]-Django rest framework change primary key to use a unqiue field
- [Django]-Django select_for_update cannot be used outside of a transaction
6👍
You can use this:
{% for group_for in request.user.groups.all %}
{% if group_for.name == 'Customers' %}
Text showed to users in group 'Customers'
{% elif group_for.name == 'Sellers' %}
Text showed to users in group 'Sellers'
{% endif %}
{% endfor %}
This is iterating through groups related to the user who makes the request and printing the text if the name of the iterated group equals ‘Customers’, ‘Sellers’, etc
- [Django]-"CSRF token missing or incorrect" while post parameter via AJAX in Django
- [Django]-Django's SuspiciousOperation Invalid HTTP_HOST header
- [Django]-ImportError: Couldn't import Django
5👍
The easiest way that I found is by adding all groups name to the context by using a context_preprocessor
In your app create a file context_processors.py
and add the following content:
def user_groups_processor(request):
groups = []
user = request.user
if user.is_authenticated:
groups = list(user.groups.values_list('name',flat = True))
return {'groups': groups}
in your settings, add the new context processor
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
# ... some options here ...
"context_processors": [
"my_app.context_processors.user_groups_processor"
],
},
},
]
Or if you prefer in settings.py
TEMPLATES[0]['OPTIONS']['context_processors'].append("my_app.context_processors.user_groups_processor")
After that in your templates you can use:
{% if 'vip' in groups %}
<p>Paragraph only visible to VIPs</p>
{% endif %}
- [Django]-Substring in a django template?
- [Django]-ImportError: No module named 'django.core.urlresolvers'
- [Django]-What's the best way to store a phone number in Django models?
- [Django]-Is it better to use path() or url() in urls.py for django 2.0?
- [Django]-Why use Django on Google App Engine?
- [Django]-Checking for empty queryset in Django
1👍
Although the answer given by mishbah is right but it didn’t work for me.
I am using Django 2.2.7 and i figured out that register = template.Library()
should be replaced with from django.template.defaultfilters import register
.
i hope someone will find it useful.
- [Django]-Django: using more than one database with inspectdb?
- [Django]-Requirements.txt greater than equal to and then less than?
- [Django]-Access request in django custom template tags
1👍
In my case the problem was, I was using {% load filter_method_name %}
I had to change to {% load filename %}
For example,
app/
__init__.py
models.py
templatetags/
__init__.py
auth_extras.py
views.py
Here, template taq will be {% load auth_extras %}
I then had to restart the server.
- [Django]-Naming convention for Django URL, templates, models and views
- [Django]-Test that user was logged in successfully
- [Django]-Can't compare naive and aware datetime.now() <= challenge.datetime_end
0👍
First You need to define a custom filter function inside has_group.py
from django import template
from xx.models import Xuser
register = template.Library()
@register.filter(name='has_group')
def has_group(user, group_name):
try:
group = Xuser.objects.get(email=user.email)
if group.role == group_name:
return True
else:
return False
except Xuser.DoesNotExist:
return False
return group
in django settings.py
file you need to add
'libraries': {
'my_templatetag': 'xx.templates.has_group',
},
inside TEMPLATES = []
and then add
{% load my_templatetag %}
in your example.html
in last
{% if user|has_group:"admin" %}
{% endif %}
- [Django]-Where is a good place to work on accounts/profile in Django with the Django registration app?
- [Django]-Django – what is the difference between render(), render_to_response() and direct_to_template()?
- [Django]-How do you configure Django for simple development and deployment?