django 的 url 配置主要在 urls.py 中进行

urlconfig 中对 url 的处理方式主要在:

一 视图处理方式

上文 例子所示:

url(r'^blog/index/$', 'blog.views.index'),

二 直接用方法名代表 url 处理方式:

from django.conf.urls import patterns, include, url
from hi.views import index # Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover() urlpatterns = patterns('',
# Examples:
# url(r'^$', 'csvt01.views.home', name='home'),
# url(r'^csvt01/', include('csvt01.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
#url(r'^hi/index/$', 'hi.views.index'),
url(r'^hi/index/$', index),
)

三 统一处理模块名的前缀

url 视图模块的前缀可以统一省略,patterns() 中的第一个参数即为前缀名:

from django.conf.urls import patterns, include, url
from hi.views import index # Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover() urlpatterns = patterns('hi.views',
# Examples:
# url(r'^$', 'csvt01.views.home', name='home'),
# url(r'^csvt01/', include('csvt01.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^hi/index/$', 'index'),
#url(r'^hi/index/$', index),
)

四 url 中传递参数

urls.py:

from django.conf.urls import patterns, include, url
from hi.views import index # Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover() urlpatterns = patterns('hi.views',
# Examples:
# url(r'^$', 'csvt01.views.home', name='home'),
# url(r'^csvt01/', include('csvt01.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^hi/index/(?P<id>\d*)/$', 'index'),
#url(r'^hi/index/$', index),
)

views.py:

from django.shortcuts import render_to_response

class Person(object):
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex def say(self):
return "This is " + self.name def index(req, id):
books = {'a':'linux in a nutshell','b':'unix programming'}
user = Person('eli', 24, 'male')
user_list = ['eli', 'lie', 'iel']
return render_to_response('index.html', {'title':'Django Sample', 'context':user, 'users':user_list, 'books':books, 'id':id})

则形如 127.0.0.1:8000/blog/index/4893/ 的地址中的 4893 将会通过正则表达式分组传递给 index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{{title}}</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="keywords" content="Django Template" />
<meta name="description" content="Django Template" />
</head>
<body>
<hi>id : {{id}}</hi>
<center>{{context.name}}</center>
<center>{{context.say}}</center>
<center>
<li>{{users}}</li>
<li>{{users.0}}</li>
</center>
{% if context %}
<li>test if : user age {{context.age}}</li>
{% else %}
user not found.
{% endif %} {% for user in users %}
<li>User: {{user}}</li>
{% endfor %} {% for k,v in books.items %}
<li>{{forloop.counter}} {{k}}: {{v}}</li>
{% endfor %}
</body>
</html>

正则参数分组时可以不命名分组数据,即 url 可以写成如下形式:

url(r'^hi/index/(\d*)/$', 'index'),

django: urlconfig的更多相关文章

  1. Django之路由配置系统(urlConfig)

    简而言之,django的路由系统作用就是使views里面处理数据的函数与请求的url建立映射关系.使请求到来之后,根据urls.py里的关系条目,去查找到与请求对应的处理方法,从而返回给客户端http ...

  2. python 培训之Django

      1.Install  sudo apt-get install python-pip sudo pip install django==1.8  2. Create Project django- ...

  3. Django视频教程 - 基于Python的Web框架(全13集)

    Django是由Python驱动的开源模型-视图-控制器(MVC)风格的Web应用程序框架,使用Django可以在即可分钟内快速开发一个高品质易维护数据库驱动的应用程序.下面是一大坨关于Django应 ...

  4. django 学习点滴

    django连接数据库要安装第三方包,比如mysql的就是 python-mysqldb, 用apt-cache search python-mysql 搜索一下. django的project可以放 ...

  5. Django中ModelForm应用

    Django中ModelForm的应用 在传统中Form提交的POST的数据在服务器端获取时将不得不一一获取并验证数据的可靠性,但是使用django提供的Form时可简化该过程并提供相应的验证,同时D ...

  6. django防止表单数据重复提交

    思路:      在Asp.net中存在Page.IsPostback的方法,所以对django中表单提交数据的重复提交的数据采用相似方法实现,即在页面第一次访问时,即访问方法为GET方法在view中 ...

  7. django Rest Framework----APIView 执行流程 APIView 源码分析

    在django—CBV源码分析中,我们是分析的from django.views import View下的执行流程,这篇博客我们介绍django Rest Framework下的APIView的源码 ...

  8. django常用封装

    #encoding:utf-8from django.shortcuts import render_to_responseimport hashlibfrom binascii import b2a ...

  9. Django——如何处理请求(URL配置和视图)

    URLconfig—— 为了绑定视图函数和URL,我们使用URLconf. URLconf 就像是 Django 所支撑网站的目录. 它的本质是 URL 模式以及要为该 URL 模式调用的视图函数之间 ...

随机推荐

  1. oracle单行函数之数字函数

    round--四舍五入函数 trunc--截断函数 mod--求余函数 round和trunc除了可以操作数字外还可以操作日期. Demo SQL),),),),) from dual ; ) ) ) ...

  2. win8\win server 2012添加【中文--美式键盘】

    1. 修改注册表 Windows Registry Editor Version 5.00 [HKEY_CURRENT_USERKeyboard Layout] [HKEY_CURRENT_USERK ...

  3. Extjs中grid表头内容居中

    在每一列中加上header属性即可,源码: header:'<div style=" text-align: center; vertical-align: middle;" ...

  4. 使用 HTML5 设计辅助功能

    使用 HTML5 设计辅助功能 Rajesh Lal 下载代码示例 如果您真的对面向广大受众感兴趣,将需要为网站设计辅助功能. 辅助功能使网页更易于访问.更易于使用,可供每个人浏览. 通常,使用最新的 ...

  5. 适用于 PHP 开发人员的 Python 基础知识

    Thomas Myer, 负责人, Triple Dog Dare Media 简介: 您是一名经验丰富的 PHP 开发人员,并且希望学习 Python 吗?本文将从 PHP 开发人员的角度来探索 P ...

  6. Python入门100例题

    原文链接:http://www.cnblogs.com/CheeseZH/archive/2012/11/05/2755107.html 无论学习哪门计算机语言,只要把100例中绝大部分题目都做一遍, ...

  7. xPath技术

    在dom4j中如何使用xPath技术 1)导入xPath支持jar包 . jaxen-1.1-beta-6.jar 2)使用xpath方法 List<Node> selectNodes(& ...

  8. TFT LCD控制显示总结(硬件概念、初始化相关配置)(转)

    源地址:http://nervfzb.blog.163.com/blog/static/314813992011215105432369/ TFT LCD是嵌入式中比较常用的显示器,S3C2440/S ...

  9. AngularJs 如何监视外部变量是否改变? 如何使用$cookieStore保存cookie?

    1. 如何监视外部变量是否改变? 如果我们要求:在$scope之外改变一个外部变量时,触发一些操作.我们可以将外部变量写进$watch中,如图中所示.返回的n表示newValue,即新的值.o表示ol ...

  10. 客户端是选择Java Swing还是C# Winform

      登录|注册     mentat的专栏       目录视图 摘要视图 订阅 [专家问答]韦玮:Python基础编程实战专题     [知识库]Swift资源大集合    [公告]博客新皮肤上线啦 ...