Section No. 304, Django Framework, urls.py module--

Source: Internet
Author: User

Django Framework, module

One, urls.py module

This module is the module that configures the route map, when the user accesses a URL address, through this route mapping module, maps to the corresponding logical processing function

A list of urlpatterns equals one of the elements in the table is a route map

urlpatterns route Map Configuration method

Parameter description:

A regular expression string
A callable object, typically a view function or a string that specifies the path of a view function
Optional default parameters to pass to the view function (in dictionary form)
An optional name parameter

Urlpatterns = [    URL (regular expression, mapping function, parameter [optional], alias [optional]),]
= [ url (r'^admin/', admin.site.urls,{'a' :'123'},'ADMIN'),]

Such as:

"""xiangmu URL configurationthe ' urlpatterns ' list routes URLs to views. For more information see:https://docs.djangoproject.com/en/1.10/topics/http/urls/examples:function views 1. ADD an import:from My_app import views 2. Add a URL to Urlpatterns:url (R ' ^$ ', views.home, Name= ' home ") class-based views 1. ADD an import:from other_app.views import Home 2. Add a URL to Urlpatterns:url (R ' ^$ ', Home.as_view (), name= ' Home ') including another URLconf 1. Import the Include () function:from django.conf.urls import URL, include 2. Add a URL to Urlpatterns:url (R ' ^blog/', include (' Blog.urls '))""" fromDjango.conf.urlsImportURL fromDjango.contribImportAdmin fromApp1ImportViewsurlpatterns=[url (r'^admin/', Admin.site.urls),#system-generated mappings                                                 #注意里面的任意一条映射匹配成功, the back is not a match URL (r'^articles/2003/$', views.special_case_2003),                           #represents the special_case_2003 function of articles/2003/this path map views Module    #URL (r ' ^articles/([0-9]{4})/$ ', views.year_archive), #表示2003可以是0-9 of any 4 digits    #URL (r ' ^articles/([0-9]{4})/([0-9]{2})/$ ', views.month_archive), #表示匹配二级目录    #URL (r ' ^articles/([0-9]{4})/([0-9]{2})/([0-9]+]/$ ', views.article_detail), #表示匹配三级目录]

Second, views.py module, the function module of route map, the requirement of logic processing route map

Note: There are two key points when customizing the mapping function

HttpResponse (String) method returns a string to the user

1, the defined function must, define a formal parameter, this form parameter receives the URL request information object, can obtain the various request information through the various methods of this form parameter

2, return information to the user, must return at the end of the function, if you want to return a string to the user, it must be returned HttpResponse method, parameter is the string to return, you need to import this method first

 from Import Render,httpresponse  # Create your views here. def special_case_2003 (request):      Print(request.method)       # gets the path    of the user request  return HttpResponse (' hello ')

Last Test.

Browser input: http://127.0.0.1:8000/articles/2003/

Section No. 304, Django Framework, urls.py module--

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.