The method for processing specific URLs in URLconf in Django framework, djangourlconf

Source: Internet
Author: User

The method for processing specific URLs in URLconf in Django framework, djangourlconf

Sometimes you have a mode to process a series of URLs in your URLconf, but sometimes you need to process a specific URL. In this case, use the linear processing method that puts special cases first in URLconf.

For example, you can consider using the URLpattern described below to add a target page to the Django management site.

Urlpatterns = patterns ('',#... ('^ ([^/] +)/([^/] +)/add/$', views. add_stage ),#...)

This will match URLs like/myblog/entries/add/AND/auth/groups/add. However, the page for adding a user object (/auth/user/add/) is special because it does not display all form fields, it displays two password fields, and so on. We can specifically point out in the view to solve this situation:

Def add_stage (request, app_label, model_name): if app_label = 'auth 'and model_name = 'user': # do special-case code else: # do normal code

However, as we have mentioned many times in this chapter, this is not elegant: because it places the URL logic in the view. The more elegant solution is to use the top-down parsing sequence of URLconf:

Urlpatterns = patterns ('',#... ('^ auth/user/add/$', views. user_add_stage), ('^ ([^/] +)/([^/] +)/add/$', views. add_stage ),#...)

In this case, requests such as/auth/user/add/will be processed by the user_add_stage view. Although the URL matches the second mode, it first matches the above mode. (This is short circuit logic .)

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.