服务器之家

服务器之家 > 正文

Python urls.py的三种配置写法实例详解

时间:2020-10-05 11:26     来源/作者:academy

urls.py的配置写法一般有三种方式。

1. 第一种是导入视图的方式,就是 The Django Book 里面样例的写法:

?
1
2
from blog.views import index
url(r'^nowamagic/', index)

 2. 第二种方法是视图处理方法,看代码就知道是怎么回事了。

?
1
url(r'^nowamagic/', 'test.views.index')

3. 第三种是把模型与视图写在前缀里。

?
1
2
3
4
urlpatterns = patterns('blog.views',  
url(r'^nowamagic$', 'index'
url(r'^nowamagic/\d{2}/$', 'index')
url(r'^nowamagic/(?P<id>\d{2})/$', 'index' )

大同小异。

下面来个详细的代码总结:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from django.conf.urls.defaults import *
 
urlpatterns = patterns('',
  # test_client modeltest urls
  (r'^test_client/', include('modeltests.test_client.urls')),
  (r'^test_client_regress/', include('regressiontests.test_client_regress.urls')),
 
  # File upload test views
  (r'^file_uploads/', include('regressiontests.file_uploads.urls')),
 
  # Always provide the auth system login and logout views
  (r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
  (r'^accounts/logout/$', 'django.contrib.auth.views.logout'),
 
  # test urlconf for {% url %} template tag
  (r'^url_tag/', include('regressiontests.templates.urls')),
 
  # django built-in views
  (r'^views/', include('regressiontests.views.urls')),
 
  # test urlconf for middleware tests
  (r'^middleware/', include('regressiontests.middleware.urls')),
 
  # admin view tests
  (r'^test_admin/', include('regressiontests.admin_views.urls')),
  (r'^generic_inline_admin/', include('regressiontests.generic_inline_admin.urls')),
 
  # admin widget tests
  (r'widget_admin/', include('regressiontests.admin_widgets.urls')),
 
  (r'^utils/', include('regressiontests.utils.urls')),
 
  # test urlconf for syndication tests
  (r'^syndication/', include('regressiontests.syndication.urls')),
 
  # conditional get views
  (r'condition/', include('regressiontests.conditional_processing.urls')),
 
  # middleware exceptions tests
  (r'middleware_exceptions/', include('regressiontests.middleware_exceptions.urls')),
 
  # special headers views
  (r'special_headers/', include('regressiontests.special_headers.urls')),
)

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://www.nowamagic.net/academy/detail/13281036

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部