码迷,mamicode.com
首页 > Web开发 > 详细

Django urls配置

时间:2016-05-12 17:34:43      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

浏览器向web服务器请求url地址时,urls.py文件中的patterns方法进行响应,响应的方法是通过url方法进行匹配的,匹配的过程中需要传入4个参数,4个参数的作用如下:
url(路径,视图,其它,name)
路径:使用regex的方式进行路径字符串匹配的
视图:是通过app下的views中的函数定义的
其它:可以传递任意参数到目标view
name:可以将此处的参数传递给特殊的templates中(templates后面再将,它是Django特有的编程模式——MVT,MVT编程模型中需要使用的,templates是专门负责web前端的显示)

e.g:
#app/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns(‘‘,
    # Examples:
    # url(r‘^$‘, ‘models_foreign.views.home‘, name=‘home‘),
    # url(r‘^blog/‘, include(‘blog.urls‘)),

    url(r‘^admin/‘, include(admin.site.urls)),
    url(r‘^$‘, views.index,name=‘index‘),
)

官方引用:
The url() function is passed four arguments, two required: regex and view, and two optional: kwargs, and name. At this point, it’s worth reviewing what these arguments are for.

url() argument: regex

The term “regex” is a commonly used short form meaning “regular expression”, which is a syntax for matching patterns in strings, or in this case, url patterns. Django starts at the first regular expression and makes its way down the list, comparing the requested URL against each regular expression until it finds one that matches.

Note that these regular expressions do not search GET and POST parameters, or the domain name. For example, in a request to https://www.example.com/myapp/, the URLconf will look for myapp/. In a request to https://www.example.com/myapp/?page=3, the URLconf will also look for myapp/.

If you need help with regular expressions, see Wikipedia’s entry and the documentation of the re module. Also, the O’Reilly book “Mastering Regular Expressions” by Jeffrey Friedl is fantastic. In practice, however, you don’t need to be an expert on regular expressions, as you really only need to know how to capture simple patterns. In fact, complex regexes can have poor lookup performance, so you probably shouldn’t rely on the full power of regexes.

Finally, a performance note: these regular expressions are compiled the first time the URLconf module is loaded. They’re super fast (as long as the lookups aren’t too complex as noted above).

url() argument: view

When Django finds a regular expression match, Django calls the specified view function, with an HttpRequest object as the first argument and any “captured” values from the regular expression as other arguments. If the regex uses simple captures, values are passed as positional arguments; if it uses named captures, values are passed as keyword arguments. We’ll give an example of this in a bit.

url() argument: kwargs

Arbitrary keyword arguments can be passed in a dictionary to the target view. We aren’t going to use this feature of Django in the tutorial.

url() argument: name

Naming your URL lets you refer to it unambiguously from elsewhere in Django especially templates. This powerful feature allows you to make global changes to the url patterns of your project while only touching a single file.

When you’re comfortable with the basic request and response flow, read part 2 of this tutorial to start working with the database.

Django urls配置

标签:

原文地址:http://blog.csdn.net/foryouslgme/article/details/51362824

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!