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\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

   

    `title`

`text`

`id`