How do I change a Django Template based on the User's Group?
Right now I have two different groups of users on my site: customers and businesses.
Right now I am only using one login that allows both user groups to see their profile page.
However, there are portions of the profile page I only want the Customer to see and portions I only want the business to see. How can I 开发者_高级运维go about limiting what each group sees on this page?
Should I do it in the template with some sort of if statement? or is there some other solution anyone can let me know about?
If you want to avoid adding anything to your view functions, and you’re using the auth context processor (django.contrib.auth.context_processors.auth
) and RequestContext
as per @thyagx’s answer, then you could use a template snippet like that suggested in this Google Groups post:
{% for group in user.groups.all %}
{% if group.name == 'customers' %}
{% comment %}Customer-specific code goes here{% endcomment %}
{% endif %}
{% endfor %}
It’s a bit verbose, but it does mean you don’t have to do anything in your view (aside from using RequestContext
), or write a custom context processor.
This is probably too old for you to care any more, but I stumbled here myself before figuring it out on my own. For posterity, I found the following solution:
In your view, add something like this:
is_customer = request.user.groups.filter(name='Customers').exists()
In your template:
{% if is_customer %} customer stuff here {% endif %}
It relies on the fact that an if
clause in a template will be evaluate to false for an empty list.
What I did to solve this is:
I created a custom context processor which basically inserts new variables for you to use in your templates and added it to my settings. See more @ https://docs.djangoproject.com/en/1.3/ref/templates/api/#django.template.RequestContext:
TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.debug', 'django.core.context_processors.i18n', 'django.core.context_processors.media', 'django.core.context_processors.static', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'common.user_context.user_context' )
I went on to write the function
user_context
inside the fileuser_context
, mine is like so:def user_context(request): if request.user.is_authenticated(): is_admin = is_local_admin(request.user) else: is_admin = False return { 'is_local_admin': is_admin }
is_local_admin
is just a function that checks if the user belongs to the Admins group or not.Whenever I need this
is_local_admin
information in my template I use this to render it in my view, for example:return render_to_response('create_user.html', { 'form': form }, context_instance=RequestContext(request))
The important part is the RequestContext
, which loads the custom context processor we build in step 1.
Now in your template you can just use:
{% if is_local_admin %}
<h1>You can see this</h1>
{% else %}
<h1>Nothing here</h1>
{% endif %}
Hope this helps someone. In summary: take a look at custom context processors, they are worth the read.
I implemented this through a template tag, based on what I found here. Perhaps it can be useful to someone.
In utils/utils_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):
try:
group = Group.objects.get(name=group_name)
except:
return False # group doesn't exist, so for sure the user isn't part of the group
# for superuser or staff, always return True
if user.is_superuser or user.is_staff:
return True
return user.groups.filter(name=group_name).exists()
Then in the template itself:
{% load utils_extras %}
{% if user|has_group:"mygroup" %}
{% if 'group_name' in user.groups.get.name %} do smth {% endif %}
精彩评论