django: urlconfig
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的更多相关文章
- Django之路由配置系统(urlConfig)
简而言之,django的路由系统作用就是使views里面处理数据的函数与请求的url建立映射关系.使请求到来之后,根据urls.py里的关系条目,去查找到与请求对应的处理方法,从而返回给客户端http ...
- python 培训之Django
1.Install sudo apt-get install python-pip sudo pip install django==1.8 2. Create Project django- ...
- Django视频教程 - 基于Python的Web框架(全13集)
Django是由Python驱动的开源模型-视图-控制器(MVC)风格的Web应用程序框架,使用Django可以在即可分钟内快速开发一个高品质易维护数据库驱动的应用程序.下面是一大坨关于Django应 ...
- django 学习点滴
django连接数据库要安装第三方包,比如mysql的就是 python-mysqldb, 用apt-cache search python-mysql 搜索一下. django的project可以放 ...
- Django中ModelForm应用
Django中ModelForm的应用 在传统中Form提交的POST的数据在服务器端获取时将不得不一一获取并验证数据的可靠性,但是使用django提供的Form时可简化该过程并提供相应的验证,同时D ...
- django防止表单数据重复提交
思路: 在Asp.net中存在Page.IsPostback的方法,所以对django中表单提交数据的重复提交的数据采用相似方法实现,即在页面第一次访问时,即访问方法为GET方法在view中 ...
- django Rest Framework----APIView 执行流程 APIView 源码分析
在django—CBV源码分析中,我们是分析的from django.views import View下的执行流程,这篇博客我们介绍django Rest Framework下的APIView的源码 ...
- django常用封装
#encoding:utf-8from django.shortcuts import render_to_responseimport hashlibfrom binascii import b2a ...
- Django——如何处理请求(URL配置和视图)
URLconfig—— 为了绑定视图函数和URL,我们使用URLconf. URLconf 就像是 Django 所支撑网站的目录. 它的本质是 URL 模式以及要为该 URL 模式调用的视图函数之间 ...
随机推荐
- Flume 1.4.0 User Guide
Apache Flume is a distributed, reliable, and available system for efficiently collecting, aggregatin ...
- Linux编程基础——GDB(设置断点)(转:TianFang,cnblog: http://www.cnblogs.com/TianFang/archive/2013/01/20/2868889.html)
启动GDB后,首先就是要设置断点,程序中断后才能调试.在gdb中,断点通常有三种形式: 断点(BreakPoint): 在代码的指定位置中断,这个是我们用得最多的一种.设置断点的命令是break,它通 ...
- 实现单实例多线程安全API问题
前阵子写静态lib导出单实例多线程安全API时,出现了CRITICAL_SECTION初始化太晚的问题,之后查看了错误的资料,引导向了错误的理解,以至于今天凌晨看到另一份代码,也不多想的以为singl ...
- MVC4,4月22日,Ninject的另外注入方式。
学习了Ninject另外两种绑定注入的方式: 1.根据属性绑定 先在特殊的实现借口类中定义属性 使用 2.根据构造函数方式绑定 学习了条件绑定方式(conditional bindi ...
- Ftp协议Socket实现
原来用WebRequest来传输文件,被人鄙视了.就弄个Socket版的,支持Active,Passive模式. 带事件日志,有时间的人可以拿去做C#版的flashfxp. public class ...
- 转:redis windows下的环境搭建
原文来自于:http://www.2cto.com/os/201204/125971.html 下载地址:https://github.com/dmajkic/redis/downloads 下载 ...
- Bundle类
1.新建一个Bundle类 Bundle bundle=new Bundle();2.Bundle类中放入数据(key-value的形式,另一个Activity里面取数据的时候,通过key值找出对应的 ...
- MCS-51单片机的指令时序
时序是用定时单位来描述的,MCS-51的时序单位有四个,它们分别是节拍.状态.机器周期和指令周期,接下来我们分别加以说明. 节拍与状态: 我们把振荡脉冲的周期定义为节拍(为方便描述,用P表示), ...
- YII增加全局函数
法1: 在使用Yii开发中我们经常会遇到一个问题,每次使用Yii的组件.扩展等,我们会像下面一样去写: <?php Yii::app()->user; Yii::app()->get ...
- 【HDOJ】1493 QQpet exploratory park
超水的动态规划.最后要对概率求Sigma. #include <cstdio> #include <cstring> #include <cstdlib> #def ...