Python系列视频教程: Django【13讲】第五讲 URL配置
1.使用url配置的三种方式
url(r'^blog/index/$', 'blog.views.index'),
url(r'^blog/mytag/$', 'blog.views.mytag'),
第一个变量是表示url形式的正则表达式,第二个变量是视图处理方法
以上是第一种方式
还有其他方式
第二种方式:
下面通过导入view方法的方式
from django.conf.urls import patterns, include, url
from django.contrib import admin
from blog.views import index
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
#url(r'^$', 'mysite.views.home', name='home'),
#url(r'^blog/', include('blog.urls')),
#url(r'^blog/index/$', 'blog.views.index'),
url(r'^blog/index/$', index),
url(r'^blog/mytag/$', 'blog.views.mytag'),
url(r'^admin/', include(admin.site.urls)),
)
测试结果:
hello Peter
- age:23
- sex:male
the Peter say:I'm Peter
- book:python
- book:java
- book:php
- book:web
第三种方式:使用前缀
from django.conf.urls import patterns, include, url
from django.contrib import admin
#from blog.views import indexadmin.autodiscover()
urlpatterns = patterns('blog.views',
# Examples:
#url(r'^$', 'mysite.views.home', name='home'),
#url(r'^blog/', include('blog.urls')),
#url(r'^blog/index/$', 'blog.views.index'),
url(r'^blog/index/$', 'index'),
url(r'^blog/mytag/$', 'blog.views.mytag'),
url(r'^admin/', include(admin.site.urls)),
)
2.正则表达式进一步使用方法
使用到index
url(r'^blog/index/\d{2}/$', 'index'),
http://127.0.0.1:8000/blog/index/1/
404 page not found
http://127.0.0.1:8000/blog/index/12/
200 正常
3.如何通过url进行参数的传递
第一种采用get的方式
?var1=xxx && var2=yyy
第二种 直接把url路径当中的某一部分进行传递
正则表达式的分组的使用
url(r'^blog/index/(?P<id>\d{2})/$', 'index'),
参数名字叫做id,参数的形式是两位数字urlpatterns = patterns('blog.views',
# Examples:
#url(r'^$', 'mysite.views.home', name='home'),
#url(r'^blog/', include('blog.urls')),
#url(r'^blog/index/$', 'blog.views.index'),
url(r'^blog/index/$', 'index'),
#url(r'^blog/index/\d{2}/$', 'index'),
#url(r'^blog/mytag/$', 'blog.views.mytag'),
#url(r'^admin/', include(admin.site.urls)),
url(r'^blog/index/(?P<id>\d{2})/$', 'index_id'),)
在views.py中增加一个新的方法index_id(req,id):
def index_id(req,id):
#user={'name':'tom','age':33,'sex':'male'}
user=Person('Cool with id',33,'male')
book_list=['python','java','php','web']
return render_to_response('index.html',{'title':'my page','user':user,'book_list':book_list,'id':id})修改index.html
body中加入下面一行
id:{{id}}
http://127.0.0.1:8000/blog/index/99/
测试结果:
hello Cool with id
- age:33
- sex:male
the Cool with id say:I'm Cool with id
- book:python
- book:java
- book:php
- book:webid:99
如果两个url规则是冲突的,那么优先级是前面的高
urlpatterns = patterns('blog.views',
# Examples:
url(r'^blog/index/$', 'index'),
url(r'^blog/index/\d{2}/$', 'index'),
url(r'^blog/index/(?P<id>\d{2})/$', 'index_id'),)
测试结果:
hello Cool
- age:23
- sex:male
the Cool say:I'm Cool
- book:python
- book:java
- book:php
- book:web
id:
在url中,
前面给参数起了一个名字id(关键字参数)
我们也可以不起名字(无参数)
url(r'^blog/index/(\d{2})/$', 'index_anony'),
def index_anony(req,param):
#user={'name':'tom','age':33,'sex':'male'}
user=Person('Cool with anonymouse param',34,'male')
book_list=['python','java','php','web']
return render_to_response('index.html',{'title':'my page','user':user,'book_list':book_list,'id':param})http://127.0.0.1:8000/blog/index/12/
hello Cool with anonymouse param
- age:34
- sex:male
the Cool with anonymouse param say:I'm Cool with anonymouse param
- book:python
- book:java
- book:php
- book:web
id:12