Django urls配置
2016-05-10 14:16
477 查看
浏览器向web服务器请求url地址时,urls.py文件中的patterns方法进行响应,响应的方法是通过url方法进行匹配的,匹配的过程中需要传入4个参数,4个参数的作用如下:
url(路径,视图,其它,name)
路径:使用regex的方式进行路径字符串匹配的
视图:是通过app下的views中的函数定义的
其它:可以传递任意参数到目标view
name:可以将此处的参数传递给特殊的templates中(templates后面再将,它是Django特有的编程模式——MVT,MVT编程模型中需要使用的,templates是专门负责web前端的显示)
官方引用:
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.
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).
When you’re comfortable with the basic request and response flow, read part 2 of this tutorial to start working with the database.
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.
相关文章推荐
- Google深度学习笔记 Logistic Classification
- 《Go语言编程入门》视频课程开课了
- Google map api 初学
- 为韩春雨老师和他的 NgAgo-gDNA 点赞
- 为韩春雨老师和他的 NgAgo-gDNA 点赞
- 换新LOGO,58集团想从分类信息向生活服务巨头蜕变?
- 【bzoj1419】Red is good 期望dp
- django中的权限控制(form增删改)
- poj 2262 Goldbach's Conjecture -- 筛法求素数打表
- Django(一)多对一模型
- Django:URL-patterns的高级使用方式与URL传递参数
- Create a Virtual Reality Game For Google Cardboard
- golang学习第二篇 开发环境的搭建
- Golang测试技术
- 龙书(Dragon book) +鲸书(Whale book)+虎书(Tiger book)
- 【转】十条有用的 Go 技术
- Go语言学习(十三)面向对象编程-继承
- Google地图的坑SupportMapFragment.getmap() returns null
- django获取指定列的数据
- 我是实践派之mongo的一主多从