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

 更新时间:2017年04月28日 15:14:12   投稿:lqh  
这篇文章主要介绍了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')),
)

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

相关文章

  • Windows64x下VScode下载过程

    Windows64x下VScode下载过程

    这篇文章主要介绍了Windows64x下VScode下载,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09
  • python压缩和解压缩模块之zlib的用法

    python压缩和解压缩模块之zlib的用法

    这篇文章主要介绍了python压缩和解压缩模块之zlib的用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • python实现批处理文件

    python实现批处理文件

    这篇文章主要为大家详细介绍了python实现批处理文件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • Python中的随机森林算法与实战

    Python中的随机森林算法与实战

    本文详细介绍了随机森林算法,包括其原理、实现步骤、分类和回归案例,并讨论了其优点和缺点,通过面向对象编程实现了一个简单的随机森林模型,并应用于鸢尾花分类和波士顿房价预测
    2025-01-01
  • Python学习之集合的常用方法总结

    Python学习之集合的常用方法总结

    集合并不是一种数据处理类型,而是一种中间类型。集合(set)是一个无序、不重复的元素序列,经常被用来处理两个列表进行交并差的处理性。本文将详细讲解集合的一些常用方法,感兴趣的可以了解一下
    2022-03-03
  • 基于pycharm导入模块显示不存在的解决方法

    基于pycharm导入模块显示不存在的解决方法

    今天小编就为大家分享一篇基于pycharm导入模块显示不存在的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • Python中用format函数格式化字符串的用法

    Python中用format函数格式化字符串的用法

    这篇文章主要介绍了Python中用format函数格式化字符串的用法,格式化字符串是Python学习当中的基础知识,本文主要针对Python2.7.x版本,需要的朋友可以参考下
    2015-04-04
  • 详解Python函数print用法

    详解Python函数print用法

    今天给大家带来的是关于Python的相关知识,文章围绕着Python print函数的用法展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • Python3.9用pip安装wordcloud库失败的解决过程

    Python3.9用pip安装wordcloud库失败的解决过程

    一般在命令行输入pip install wordcloud 总会显示安装失败,所以下面这篇文章主要给大家介绍了关于Python3.9用pip安装wordcloud库失败的解决过程,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • Python 爬虫图片简单实现

    Python 爬虫图片简单实现

    这篇文章主要介绍了Python 爬虫图片简单实现的相关资料,需要的朋友可以参考下
    2017-06-06

最新评论