Django中的URL配置和模板

来源:互联网 发布:低调桌面时钟软件 编辑:程序博客网 时间:2024/05/18 02:29

转自:http://sinfrancis.iteye.com/blog/578091

    博客分类:
  • Django
Django正则表达式OSPythonHTML 

Django中的URL配置 :

 

实例:

 

Python代码  收藏代码
  1. urlpatterns = patterns('',  
  2.     # Example:  
  3.     # (r'^myweb/', include('myweb.foo.urls')),  
  4.   
  5.     # Uncomment the admin/doc line below and add 'django.contrib.admindocs'   
  6.     # to INSTALLED_APPS to enable admin documentation:  
  7.     # (r'^admin/doc/', include('django.contrib.admindocs.urls')),  
  8.   
  9.     # Uncomment the next line to enable the admin:  
  10.     (r'^admin/', include(admin.site.urls)),  
  11.     (r'^$',"view.index"),  
  12.     (r'^hello/$',"view.hello"),  
  13.     (r'^time/(\d+)/$',"view.time"),  
  14.     (r'^time1/(?P<id>(\d+))/$',direct_to_template,{"template":"time1.html"}),  
  15.     (r'^time2/$',direct_to_template,{'template':'time2.html'}),  
  16.        
  17. )  

 

(r'^$',"view.index"),
首页,直接访问地址,交给view.index这个方法处理,比如 :http://localhost/

(r'^hello/$',"view.hello"),
访问比如http://localhost/hello的地址交给view.hello方法处理

 

(r'^time/(\d+)/$',"view.time"),

访问http://localhost/time/1/,http://localhost/time/2/这样的地址,后面(\d+)用于匹配的数字,非数字是必能匹配的,并且后面的(\d+)的值会作为参数,所以方法应该这样写

 

def time(request,offset)  request为请求对象会自动传递进来,offset即为URL中(\d+)匹配的值,比如http://localhost/time/2/,offset 的值就是2

 

   (r'^time1/(?P<id>(\d+))/$',direct_to_template,{"template":"time1.html"}),

direct_to_template:为转发模板到 time1.html

(?P<id>(\d+)) 表示匹配后给这个参数加上一个别名,在页面中使用{{params.id}}可以访问到URL中id的值

 

另外在加载模板的时候需要配置:

1、settings.py 中的

 

Python代码  收藏代码
  1. TEMPLATE_DIRS = (  
  2.     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".  
  3.     # Always use forward slashes, even on Windows.  
  4.     # Don't forget to use absolute paths, not relative paths.  
  5.     os.path.join(os.path.dirname(__file__), 'templates/').replace('\\','/'),  
  6. )  

 表示将网站目录下 templates/的模板路径

 

2、在urls.py中导入direct_to_template方法 from django.views.generic.simple import direct_to_template

 

 

 

如何启用Django的admin,我用的是1.1.1版本:

 

1、 在setting.py中启用admin和auth

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',

)

2、在urls.py中启用以下代码:

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

url配置 (r'^admin/', include(admin.site.urls)), 启用

 

就能打开admin,比如http://localhost/admin/

 

 

 

命名匹配: 

 

在 Python 正则表达式里, 命名分组的语法是 (?P<name>pattern), 这里name 是分组的名字而 pattern 是要匹配的模式.

下面用命名分组重写上面的例子:

urlpatterns = patterns('',    (r'^articles/2003/$', 'news.views.special_case_2003'),    (r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'),    (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.month_archive'),    (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'news.views.article_detail'),)

上面的代码与前面的代码功能完全相同, 不过传递给 view 函数的不再是位置相关参数,而变成了关键字参数.比如:

 

方法定义:

def month_archive(request,year)

def month_archive(request,year,month)

def article_detail(request,year,month,day)

  • 页面请求 /articles/2005/03/ 会自动调用函数news.views.month_archive(request,year='2005', month='03'), 而不是news.views.month_archive(request,'2005', '03').
  • 页面请求 /articles/2003/03/3/ 会自动调用函数news.views.article_detail(request,year='2003', month='03', day='3').

实际上, 这意味着 URLconfs 更清晰,而且避免了参数顺序错误引发的 bug -- 定义 view 函数时不必特别在意参数的顺序.当然有些开发人员认为命名分组的语法很丑并且繁琐

0 0