问题
I created a custom auth permission in django via admin site, and i added that permission to a user (not a group), now i want to ask if the request user in a template has it but nothing works.It's not a duplicate, i already checked similar questions and none of this work:
{% if perms.auth.add_something %}
{% if 'auth.add_something' in request.user.get_all_permissions %}
{% if request.user.has_perm('add_something') %}
I add my view:
class NotificationSelectView(View):
template = 'myapp/notification_read.html'
def get(self, request, *args, **kwargs):
t = tree(request)
req_var_list = []
analist = notifications_list(request.user)
level = LevelProcess.objects.get(
level_hierarchical = 3
)
subprocess= Process.objects.filter(level = level)
user = request.user
t.update({
'subprocess': subprocess,
'analist':analist,
})
return render(request, self.template, t)
The idea it's to make it inside template, not to use more code in views. Any ideas ?, thanks in advance.
回答1:
django in template it uses the variable perms for the permissions of the logged in user so you can use inside template
{% if perms.auth.add_something %}
{{do_smth}}
{% endif %}
from the django documentation there is a section Authentication data in templates which describes also permissions in templates
来源:https://stackoverflow.com/questions/39905210/django-user-permission-inside-template