This article mainly introduces how to use multiple view prefixes in Django's URLconf. Django is the most famous development framework in Python that follows the MVC structure, if you use string technology, especially if your URLconf does not have a public prefix, you may eventually mix the view. However, you can still use the simple method of View prefix to reduce duplicates. You only need to add multiple patterns () objects, as shown in the following code:
Old:
From django. conf. urls. defaults import * urlpatterns = patterns ('', (r' ^ hello/$ ', 'mysite. views. hello '), (r' ^ time/$', 'mysite. views. current_datetime '), (r' ^ time/plus/(\ d {1, 2})/$', 'mysite. views. hours_ahead '), (r' ^ tag/(\ w +)/$', 'weblog. views. tag '),)
NEW:
From django. conf. urls. defaults import * urlpatterns = patterns ('mysite. views ', (r' ^ hello/$', 'Hello'), (r' ^ time/$ ', 'current _ datetime '), (r' ^ time/plus/(\ d {1, 2})/$ ', 'hours _ ahead'),) urlpatterns + = patterns ('weblog. views ', (r' ^ tag/(\ w +)/$', 'tag '),)
The entire framework focuses on the existence of a module-level variable named urlpatterns. In the preceding example, this variable can be dynamically generated. Here we need to note that the objects returned by patterns () can be added. this feature may not be thought.