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. centos安装redis-3.2.3

    这次介绍的是在虚拟机centos下安装redis-3.2.3 首先进入官网http://redis.io/download

  2. ubuntu自动挂载windows分区和开机自动启动wallproxy

    1. 自动挂载windows分区 ubuntu默认是要点一下相应的盘符才会挂载windows分区的. 今天发现了ubuntu下最简单的自动挂载windows分区的办法.... :) 参考如下方法:ht ...

  3. JQUERY1.9学习笔记 之基本过滤器(五) 大于选择器

    大于选择器:jQuery( ":gt(index)" )jQuery( ":gt(-index)" ) 例:大于TD5 到TD8 用黄色背景,TD8用红色文字. ...

  4. PHPCMS v9 导航显示二级菜单,显示相邻栏目,内容页显示二级栏目

    导航显示二级栏目 <div class="menu">{pc:content action="category" catid="0&quo ...

  5. php过滤参数特殊字符防注入

    分享一例php实现过滤提交的参数数据以防止注入的代码,有需要的朋友参考下. 本节内容: php过滤特符字符,php防注入. in: 后端程序 例子: 代码示例: <?php /** * 安全防范 ...

  6. python查看模块及相关函数帮助

    Question: 如何查看正则表达式模块re及其相关函数的意义 1.终端命令行下 python >> import re >> help(re) Help on module ...

  7. django初探

    如果是自己建站耍的话,还是用Php方便,毕竟Php服务器便宜又到处都是. 但是python毕竟是一个新鲜的东西,特别是django,以前一直东python的语法,而且是我最早学习的语言之一,但是一直停 ...

  8. OpenStack JEOS 镜像

    JEOS:Just Enough Operating System 维基百科地址:http://en.wikipedia.org/wiki/Just_enough_operating_system O ...

  9. SQL语句中output的用法

    在SQL语句中,output可以作为返回值来使用, 1.我们先看这个存储过程 代码: 1 set ANSI_NULLS ON  2 set QUOTED_IDENTIFIER ON  3 go  4 ...

  10. P1337 fibonacci数列(tyvj)

    http://www.tyvj.cn/p/1337 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 著名的斐波那契数列f[n]=1               ...