Home > Article > Backend Development > Solution to the problem that Django templates cannot use perms variables
This article mainly introduces to you the method to solve the problem that Django template cannot use perms variables. The article introduces it in detail through sample code. It has certain reference learning value for everyone's study or work. Friends who need it can follow Let’s learn together with the editor.
Preface
This article mainly introduces to you the solution to the problem that Django template cannot use perms variables, and shares it for your reference and study. Below Not much to say, let’s take a look at the detailed introduction.
Solution:
First of all, when using Django’s built-in permission management system, add
to the settings.py file
INSTALLED_APPS添加: 'django.contrib.auth', MIDDLEWARE添加: '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 to check permissions in templates?
According to the official website instructions https://docs.djangoproject.com/en/1.11/topics/auth/default/#permissions, the logged in user permissions are saved in the template {{ perms }}
variable is an instance of the permission template agent django.contrib.auth.context_processors.PermWrapper
. For details, you can view the django/contrib/auth/context_processors.py source code
Test case:
During the test, it was found that the {{ perms }}
variable did not exist at all and there was no output; Well, I can only get 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 printing permission information, and some do not, it seems that I suddenly woke up
The interface that can print permission information returns:
return render(request, 'fms/fms_add.html', {'request': request, 'form': form, 'error': error})
The new interface that cannot print permission information returns:
return render_to_response( 'fms/fms.html', data)
The difference between render and render_to_response
render is a more convenient method of rendering templates than render_to_reponse, and will automatically use RequestContext, while the latter requires Manually add:
return render_to_response(request, 'fms/fms_add.html', {'request': request, 'form': form, 'error': error},context_instance=RequestContext(request))
where RequestContext is a subclass of django.template.Context
. Accepts request
and context_processors
, so the problem of rendering context filling to the template has been made clear. Due to the use of the render_to_response
method, the template cannot be used without manually adding context_instance=RequestContext(request)
{{ perms }}Variables
The above is the detailed content of Solution to the problem that Django templates cannot use perms variables. For more information, please follow other related articles on the PHP Chinese website!