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 模式调用的视图函数之间 ...
随机推荐
- MFC 堆栈溢出 test dword ptr [eax],eax ; probe page.
今天调试程序的时候,发现一个奇怪的问题,之前调试都没问题的,今早加了一点东西,就出现错误,跳到调试位置,如下4行红色部分 ; Find next lower page and probe cs20: ...
- 关于<%@ include file=" " %>与<jsp:include page=""></jsp:include>中的那些问题?
今天在使用<%@ include file=" " %>指令时,竟然在页面中不让使用?这是怎么回事:问题如下图: 顿时被这个问题给搞到了!!!突然想到在以前的 JSP ...
- thinkphp基础入门(2)
第一节介绍了thinkphp基本路径问题,第二节将介绍thinkphp的常见用法(M层跟V层) 我们先在Controller层新建个IndexController.class.php(新建文件的格式为 ...
- JavaScript简易日历
<!DOCTYPE html PUBLIC "-//W3C//h2D XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Centos7 wget和普通下载有区别
今天下的禅道 wget和用win下载之后再ssh传过去,效果不一样 wget不能正常启动禅道.回来要探讨一下wget的不同之处,先记下来
- Notepad++ Java开发环境配置
1. 安装JDK 下载JDK 6下载 http://developers.sun.com.cn/download/java_se.html 运行安装程序,按照屏幕提示完成JDK 6的安装,下面为安装路 ...
- 浅谈 “空指针、野指针、void*”
Author: JW. Zhou Date: 2014/7/2 一.空指针(0/NULL) 返回NULL和返回0是完全等价的,因为NULL和0都表示空指针,换句话说:空指针是什么,就是 ...
- css、js的相互阻塞
先决条件:脚本前面存在外部样式 以下试验虽然是在chrome下,但是对于IE8+以及其他浏览器也适用. 1.内联脚本(http://jsbin.com/mudab/1) <!DOCTYPE ht ...
- Activity的四种状态
Running状态:一个新的Activity启动入栈后,它在屏幕最前端,处于栈的最顶端,此时它处于可见并可和用户交互的激活状态.Paused状态:当Activity被另一个透明或者Dialog样式的A ...
- C51汇编语言完整源码
单片机最小系统,两位LED数码管由串口输出接两个164驱动,Lout,Rout为左右声道输出,SET, ALT0, ALT1为三个按键,也可自己在开始的I/O定义改成你想用的I/O口:12M晶振,若 ...