How to use multiple view prefixes in Django URLconf

Source: Internet
Author: User
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.

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.