Solve the problem that the Django template cannot use the perms variable, djangoperms
Preface
This article describes how to solve the problem that the Django template cannot use the perms variable. I will share the solution for your reference and learning. I will not talk about it here. Let's take a look at the detailed introduction.
Solution:
First, add the settings. py file when using the built-in permission management system of Django.
Added INSTALLED_APPS: 'django. contrib. auth ', MIDDLEWARE added: 'django. contrib. auth. middleware. authenticationMiddleware ', 'django. contrib. auth. context_processors.auth ', TEMPLATES = [{'backend': 'django. template. backends. django. djangoTemplates ', 'dirs': [OS. path. join (BASE_DIR, 'templates')], 'app _ dirs': True, 'options': {'Context _ processors ': ['django. template. context_processors.debug', 'django. template. context_processors.i18n ', 'django. template. context_processors.media ', 'django. template. context_processors.static ', 'django. template. context_processors.tz ', 'django. contrib. messages. context_processors.messages ', 'django. template. context_processors.request ', 'django. contrib. auth. context_processors.auth ',] ,},},]
How do I check the permissions in the template?
According to official website instructions https://docs.djangoproject.com/en/1.11/topics/auth/default/#permissions, logged-on user permissions are saved in the template{{ perms }}
In the variable, it is the permission template proxy.django.contrib.auth.context_processors.PermWrapper
You can view the django/contrib/auth/context_processors.py source code.
Test cases:
During the test{{ perms }}
The variable does not exist at all, and there is no output. Well, you can only use the source code of Debug Django.
def auth(request): """ Returns context variables required by apps that use Django's authentication system. If there is no 'user' attribute in the request, uses AnonymousUser (from django.contrib.auth). """ if hasattr(request, 'user'): user = request.user else: from django.contrib.auth.models import AnonymousUser user = AnonymousUser() print(user, PermWrapper(user), '-----------------------') return { 'user': user, 'perms': PermWrapper(user), }
Test the access interface and find that some interfaces have printed permission information, while others do not.
The interface that can print the permission information returns:
return render(request, 'fms/fms_add.html', {'request': request, 'form': form, 'error': error})
The new permission cannot be printed:
return render_to_response( 'fms/fms.html', data)
Difference between render and render_to_response
Render is a more convenient method for rendering templates than render_to_reponse. RequestContext is automatically used, while the latter must manually add:
return render_to_response(request, 'fms/fms_add.html', {'request': request, 'form': form, 'error': error},context_instance=RequestContext(request))
Where RequestContext isdjango.template.Context
Subclass. Acceptrequest
Andcontext_processors
To render the context filling to the template.render_to_response
Method, not manually addedcontext_instance=RequestContext(request)
The template cannot be used.{{ perms }}
Variable
Summary
The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.