Solve the problem that the Django template cannot use the perms variable, djangoperms

Source: Internet
Author: User

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.PermWrapperYou 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.ContextSubclass. AcceptrequestAndcontext_processorsTo render the context filling to the template.render_to_responseMethod, 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.