码迷,mamicode.com
首页 > Web开发 > 详细

Django中的URL配置和模板

时间:2015-05-13 23:24:53      阅读:384      评论:0      收藏:0      [点我收藏+]

标签:

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‘).

Django中的URL配置和模板

标签:

原文地址:http://my.oschina.net/yusi/blog/414534

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