[Django] design of the user permission Learning Series Self-owned permission management system design idea, django permission management system
If you encounter permission operation problems when reading this article, please refer to the first two chapters of this series!
Bytes
This article describes the idea of designing a self-owned permission management system. The self-owned permission management system discards the background management interface provided by django, manage user permissions in the Self-compiled permission management field!
First: (self-designed permission system interface, the code will be written in subsequent articles)
The permission management interface is used to add and delete permissions. You can see from the official documentation that permission has three fields: content_type_id (permission type), codename (permission name), and name (permission description)
View, we can see that the permission name is displayed in Chinese, django background comes with English, such:
Of course, the Chinese is added by myself. The advantage of using Chinese is that you can use regular expressions in the background to filter out Chinese-only permissions, in this way, the user does not need to display the English permission, and the Chinese regular expression is [\ u4e00-\ u9fa5].
The following code queries the Chinese permission in the background view:
# Matching Chinese permissions (single query) c = Permission. objects. get (codename _ iregex = U' [\ u4e00-\ u9fa5] ') print c # match Chinese Permission (all queries) Permission. objects. filter (codename _ iregex = U' [\ u4e00-\ u9fa5] '). values ()
The user management interface adds and assigns permissions to users, for example:
Some may ask how to control the permissions of a webpage or operation after user permissions are configured?
In fact, the code is similar to that on the django official website. The permission verification code in the template is as follows:
{% If perms. auth. business Development %} <p class = "text-center"> <a href = "{% url 'keywork' %}"> business development </a> </p> {% else %} <p class = "text-center"> <a href = "javascript: alert ('no permission. Please contact the administrator! ') "> Business development </a> </p>
{% Endif %}
View to check permissions:
U = User. objects. get (username = 'account') u. has_perm (u "auth. \ u4e1a \ u52a1 \ u53d1 \ u5c55 ")
In the preceding example, you must first use B = list (User. objects. get (username = 'account'). get_all_permissions () to view the code displayed in pyhton.
The specific production process will be carried out in subsequent articles. In order to restore the process of developing a permission system for everyone, rather than simply sticking a large code (ps: I used )......