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. 网络编程 socket-实例

    1.设计界面:   2.效果界面:   3.具体实现代码:   public partial class frmMain : Form { public frmMain() { InitializeC ...

  2. libusb简介

    概述 libusb是一个C库,它提供了通用的访问USB设备. 它的目的是供开发人员使用方便的生产与USB通信硬件的应用程序. 可移植的: 使用一个跨平台API,它提供了访问USB设备在Linux上,O ...

  3. 一个简单的javascript获取URL参数的代码

    function request(paras){ var url = location.href; var paraString = url.substring(url.indexOf("? ...

  4. [Python 3.x 官方文档翻译]The Python Tutorial Python教程

    Python is an easy to learn, powerful programming language. It has efficient high-level data structur ...

  5. MacOS上的包管理工具

    众所周知,Linux世界中存在着如apt-get.yum等强大而简单的包管理工具,而在Macos中同样存在类似的东西. 当前比较流行的主要有如下两种: 1.Homebrew(推荐):其官网地址brew ...

  6. AndroidStudio1.1.0配置使用androidannotations

    1:从GitHub上下载最新版androidannotations-api-3.3.1.jar 2:新建Module:my-aa-test 3:将androidannotations-api-3.3. ...

  7. BroadcastReceiver 案例

    BroadcastReceiver 可以接收来自系统和应用的广播,他的生命周期非常简单,只是从对象开始调用他到运行onReceiver方法之后就结束了.要想使用BroadcastReceiver和使用 ...

  8. 在线安装maven插件问题:Cannot complete the install because one or more required items could not be found.

    用Eclipse在线安装的方式:Help-->Install  New Software 地址输入:http://m2eclipse.sonatype.org/sites/m2e/,列表中打勾勾 ...

  9. Javascript OrderBy

    要在js 实现orderBy基本知识就是 array.sortarray.sort(function(a,b){ a 表示 row 0 b 表示 row 1 它会loop多次你可以比较 if(a &g ...

  10. rsyslog kill 测试重发例子

    [root@dr-mysql01 zjzc_log]# >zj-frontend01-error.2016-09-26 [root@dr-mysql01 zjzc_log]# [root@dr- ...