Django-- routing urls

1. What is the urls?

  Like the Django directory site support. Its essence is a mapping table between the URL and the URL you want to call that view function; you're in such a way to tell Django, the client sent to a URL which calls the corresponding period of logic code execution.

2, a simple routing configuration

from django.conf.urls import url,include


from app01 import views
urlpatterns = [
    url(r'datatimes/(\d{4})/(\d{2})$', views.year_pipei),
]
  • To capture a value from the URL, you only need to place a pair of parentheses around it.
  • No need to add a leading backslash, because each URL has. For example, it should be ^articles instead  ^/articles.
  • Each regular expression in front of the 'r' is optional, but suggested adding. It tells Python that the string is "primitive" - ​​the string of characters should not be any escape

3, well-known group

  The above example uses simple regex unnamed group (by the parentheses) to capture the value of the URL and the position parameter passed to the view. In more advanced usage, you can use named regular expression groups to capture value in the URL and keyword parameters passed to the view.

In Python regular expressions, regular expression syntax group name is (?P<name>pattern), where name is the name of the group, pattern is the pattern to match.

Here are used the above named group URLconf rewritten:

from django.conf.urls import url,include


from app01 import views
urlpatterns = [
    url(r'datatimes/(?P<year>\d{4})/(?P<month>\d{2})$', views.year_pipei)
]

  This implementation is identical to the previous example, only a subtle difference: the captured value as a key instead of the position parameter transfer function parameters to view. E.g:

  / Articles / 2005/03 / requesting calls views.month_archive (request, year = '2005', month = '03 ') function instead views.month_archive (request,' 2005 ',' 03 ').

4, distribution

  If a project has multiple app, how do we determine a route which app to perform? So we used the route of distribution, we need to create a file in urls.py each app, and then write in the global urls.py in:

from django.conf.urls Import URL, the include
 from django.contrib Import ADMIN
 from app02 Import views   # if the following two views are introduced into the app will cover above 


the urlpatterns = [ 
    URL (R & lt ' ^ ADMIN / ' , admin.site.urls), 
    URL (R & lt ' ^ app01 / ' , the include ( ' app01.urls ' )), # refers to the route beginning app01, will be distributed in the views.py app01 
    URL (R & lt ' ^ app02 / ' , the include ( ' app02.urls ' )), 
]

  So that we can address by routing the beginning of the app01 app01 performed in urls.py, app02 in urls.py. execution routing address beginning app02

5, reverse analysis

   Login.html the following code in the template:

<form action="/index/"method="post">
        用户名<input type="text" name="user">
        密码<input type="password" name="pwd">
        <input type="submit">

  That is, after the trigger event, will jump to the route for the index page, but if the route index is modified, can not be used here, then have to modify to perform normal route here, it is clear that this is very troublesome, then we the need to reverse analysis,

  In urls.py add to the routing in the name attribute:

 url(r'index', views.login, name='xxx')

  Give the template to read as follows:

<form action="{% url 'xxx' %}"method="post">
        用户名<input type="text" name="user">
        密码<input type="password" name="pwd">
        <input type="submit">

  So that regardless of any future change routing index form, and more will be passed to the template name, this is the reverse lookup.

Guess you like

Origin www.cnblogs.com/490144243msq/p/11570026.html