How Django URLs are configured

Source: Internet
Author: User

An example of the blog application in the above essay.

The default generated format for urls.py is as follows:

1 """whsweb URL Configuration2 3 The ' urlpatterns ' list routes URLs to views. for more information. See:4 https://docs.djangoproject.com/en/1.8/topics/http/urls/5 Examples:6 Function views7 1. Add an import:from my_app import views8 2. Add a URL to Urlpatterns:url (R ' ^$ ', views.home, name= ' home ')9 class-based viewsTen 1. Add an import:from other_app.views import Home One 2. Add a URL to Urlpatterns:url (R ' ^$ ', Home.as_view (), name= ' Home ') A including another URLconf - 1. Add An Import:from blog import URLs as Blog_urls - 2. Add a URL to Urlpatterns:url (R ' ^blog/', include (Blog_urls)) the """ -  fromDjango.conf.urlsImportinclude, url -  fromDjango.contribImportAdmin -  +Urlpatterns = [ -URL (r'^admin/', include (Admin.site.urls)), +]
The first way: Apply. View. Methods

URL (r ' ^blog/index/$ ', ' blog.views.archive ')

This means that the access URL matches the R ' ^blog/index/$ ' regular expression, the Web page renders the archive method in the Views view of the blog application

Second Use method: Call Object

Head Import object:

From Blog.views Import Archive

URL (r ' ^blog/index/$ ', archive)

Third Way: Build patterns Assignment

Head Remember Import

From Django.conf.urls import include, url,patterns
Assigning a value to a Urlpatterns object using the Patterns method
Urlpatterns =patterns ('blog.views'  url (r'^blog/index/$  ','archive')               )
Fourth way: include other URLs
The URL (r ' ^blog/', include (' Blog.urls ')) directly contains the URLs configuration under the blog.
Url method:
defURL (regex, view, Kwargs=none, Name=none, prefix="'):    ifisinstance (view, (list, tuple)):#For include (...) processing.Urlconf_module, app_name, Namespace =ViewreturnRegexurlresolver (Regex, Urlconf_module, Kwargs, App_name=app_name, namespace=namespace)Else:        ifisinstance (View, six.string_types): Warnings.warn ('Support for string view arguments to URL () is deprecated and'                ''ll is removed in Django 2.0 (got%s). Pass the callable'                'instead.'%view, removedindjango20warning, Stacklevel=2            )            if  notView:RaiseImproperlyconfigured ('Empty URL Pattern view name not permitted (for pattern%r)'%regex)ifPrefix:view= prefix +'.'+ViewreturnRegexurlpattern (Regex, view, Kwargs, name)

You can see that the URL parameter can be configured in three ways: 1, List 2, tuple 3, string

The Include method returns a tuple

How Django URLs are configured

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.