Get all the URLs of the Django project and the Django project url

Source: Internet
Author: User

Get all the URLs of the Django project and the Django project url

When adding permissions to a project, you may encounter a problem: setting permissions for all the URLs of the project, but it is too troublesome to enter permissions manually one by one. Therefore, you need to use code to obtain all the URLs of a project.

First, take into account the urlpartterns of the outermost layer of the project, because all URLs must be

urlpatterns = [    # url(r'^admin/', admin.site.urls),    url(r'^arya/', site.urls),    url(r'^index/', index),]

Print the list cyclically and view the result:

<RegexURLResolver <RegexURLPattern list> (arya:arya) ^arya/><RegexURLResolver <module 'rbac.urls' from 'C:\\Users\\zhangcan\\Desktop\\pro_crm\\pro_crm\\rbac\\urls.py'> (None:None) ^rbac/><RegexURLPattern None ^index/>

As you can see, the nested functions with corresponding functions and imported through files are printed out of different types. Import this type.

from django.urls.resolvers import RegexURLPatternfrom django.urls.resolvers import RegexURLResolver

Press ctrl and click to view the source code.

self._regex = regex

Print this in the code and get it:

^arya/^rbac/^index/

Wow, that's wonderful, but it can only be the last one. The above two are nested. Let's identify them by the two types obtained above.

For this nested type, you can use recursion to open it layer by layer until the RegexURLPattern type is obtained.

In addition, for each layer opened, the previous url must be added to

We can use item. urlconf_name to obtain the urlpartterns layer in the nested

Another problem involved here is how to save the results each time using recursive functions?

You can use a global variable. In fact, you can also use a Python feature. If the default value of a parameter is a dictionary and the list is a variable data type, the same memory address will be referenced later.

Therefore, this global variable can also be written as a parameter with an empty list by default, but this creates a new problem: as long as the project is not restarted, the list will not be cleared, therefore, the default parameter "False" is used. When this parameter is called for the first time, it is set to "True". In the function, if this parameter is set to "True", the list is cleared.

The final code can be written as follows:

From django. conf. urls import url, includefrom arya. service. sites import sitefrom django. urls. resolvers import RegexURLPatternfrom django. urls. resolvers import RegexURLResolverfrom django. shortcuts import HttpResponsedef index (request): print (get_all_url (urlpatterns, prev = '/') return HttpResponse ('... ') def get_all_url (urlparrentens, prev, is_first = False, result = []): if is_first: result. clear () for item in urlparrentens: v = item. _ regex. strip ('^ $') # Remove the ^ and $ if isinstance (item, RegexURLPattern) in the url: result. append (prev + v) else: get_all_url (item. urlconf_name, prev + v) return resulturlpatterns = [url (R' ^ arya/', site. urls), url (R' ^ index/', index),]

In this way, all the URLs of this project are obtained.

['/arya/login/', '/arya/logout/', '/arya/app01/department/', '/arya/app01/department/add/', '/arya/app01/department/(.+)/delete/', '/arya/app01/department/(.+)/change/', '/arya/app01/userinfo/', '/arya/app01/userinfo/add/', '/arya/app01/userinfo/(.+)/delete/', '/arya/app01/userinfo/(.+)/change/', '/arya/rbac/userinfo/', '/arya/rbac/userinfo/add/', '/arya/rbac/userinfo/(.+)/delete/', '/arya/rbac/userinfo/(.+)/change/', '/arya/rbac/role/', '/arya/rbac/role/add/', '/arya/rbac/role/(.+)/delete/', '/arya/rbac/role/(.+)/change/', '/arya/rbac/permission/', '/arya/rbac/permission/add/', '/arya/rbac/permission/(.+)/delete/', '/arya/rbac/permission/(.+)/change/', '/arya/rbac/menu/', '/arya/rbac/menu/add/', '/arya/rbac/menu/(.+)/delete/', '/arya/rbac/menu/(.+)/change/', '/index/']

 

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.