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 .)