码迷,mamicode.com
首页 > 编程语言 > 详细

二、Python Django的URL设置

时间:2016-09-08 18:42:29      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:python django的url设置

Python Django的URL设置

一、url使用方式

1、正则表达方式: 

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


2、导入的方式: 

from blog.views import index

.....

               url(r‘^blog/index/$‘,index),

.....

3、

urlpatterns = patterns(‘blog.views‘,

     url(r‘^blog/index/$‘,‘index‘),

)



二、URL参数传递给index

添加template目录:

# vim setting.py

   TEMPLATE_DIRS = (

"blog/template",

   )


关键字传递 

url(r‘^blog/index/(?P<id>\d{2})/$‘,‘blog.views.index‘),  

# cat blog/views.py

from django.shortcuts import render_to_response

def index(req,id):

    user = {‘name‘:‘tom‘,‘age‘:23,‘sex‘:‘male‘}

    book_list = [‘python‘,‘java‘,‘php‘,‘web‘]

   return render_to_response(‘index.html‘,{‘title‘:‘Loyu page‘,‘book_list‘:book_list,‘user‘:user,‘id‘:id,‘text‘:‘I LOVE XIAOXIAO‘})

 

 

未定义参数 

url(r‘^blog/index/(\d{2})/$‘,‘blog.views.index‘),

# cat blog/views.py

from django.shortcuts import render_to_response

def index(req,loyu):

    user = {‘name‘:‘tom‘,‘age‘:23,‘sex‘:‘male‘}

    book_list = [‘python‘,‘java‘,‘php‘,‘web‘]

    return render_to_response(‘index.html‘,{‘title‘:‘Loyu page‘,‘book_list‘:book_list,‘user‘:user,‘id‘:loyu,‘text‘:‘I LOVE XIAOXIAO‘})

 

# cat blog/template/index.html

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0.5 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />

    <title>{{title}}</title>

</head>

<body>

{{text}}

{{id}}

</body>

</html>


本文出自 “流星宇” 博客,请务必保留此出处http://8789878.blog.51cto.com/8779878/1850738

二、Python Django的URL设置

标签:python django的url设置

原文地址:http://8789878.blog.51cto.com/8779878/1850738

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!