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

大家快瞧那结冰的湖水,春风一吹冰都融化了,河里的小鱼都游出海面,在湖水中自由的游来游去,多可爱的小鱼啊!多么欢快的小鱼啊!真是让人深深的为它那可爱的摸样着迷。春风一吹过那枯黄的小草身边时,小草突然边了摸样,它从原来的枯黄变成了嫩绿,慢慢地小草从嫩绿变成了鲜绿渐渐地越变越绿,绿的让人难以相信那是小草。啊!小草我为你的样子感到是个奇迹。

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

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

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

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

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

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

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

大同小异。

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

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')),
)

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

到此这篇关于Python urls.py的三种配置写法实例详解就介绍到这了。人在什么阶段,就做那个阶段的事,说这个阶段该说的话,不要想的太多,太好。不管是理想还是梦想,都是需要地基的。只有你把这个阶段做好做完美,才会能接触到下一阶段的人,下一阶段的事,才能有个明确,真实,看得见摸得着的未来。更多相关Python urls.py的三种配置写法实例详解内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python3内置模块之json编码解码方法详解