Django中的URL配置和模板
来源:互联网 发布:低调桌面时钟软件 编辑:程序博客网 时间:2024/05/18 02:29
转自:http://sinfrancis.iteye.com/blog/578091
- 博客分类:
- Django
Django中的URL配置 :
实例:
- urlpatterns = patterns('',
- # Example:
- # (r'^myweb/', include('myweb.foo.urls')),
- # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
- # to INSTALLED_APPS to enable admin documentation:
- # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
- # Uncomment the next line to enable the admin:
- (r'^admin/', include(admin.site.urls)),
- (r'^$',"view.index"),
- (r'^hello/$',"view.hello"),
- (r'^time/(\d+)/$',"view.time"),
- (r'^time1/(?P<id>(\d+))/$',direct_to_template,{"template":"time1.html"}),
- (r'^time2/$',direct_to_template,{'template':'time2.html'}),
- )
(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 中的
- TEMPLATE_DIRS = (
- # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
- # Always use forward slashes, even on Windows.
- # Don't forget to use absolute paths, not relative paths.
- os.path.join(os.path.dirname(__file__), 'templates/').replace('\\','/'),
- )
表示将网站目录下 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 函数时不必特别在意参数的顺序.当然有些开发人员认为命名分组的语法很丑并且繁琐
- Django中的URL配置和模板
- Django-视图和URL配置
- Django:视图和URL配置
- Django -- 视图和URL配置
- Django 视图和url配置
- django视图和URL配置
- Django的视图和URL配置
- Django 初识 - 视图和url配置
- django 高级视图和URL配置
- Django高级视图和URL配置
- Django中视图和URL配置
- Django框架--视图和URL配置
- django框架-视图和URL配置
- django的视图和URL配置
- Django学习(3):URL配置及模板
- Django 中的Url、HTTP机制和视图
- Django静态资源和模板配置
- django中url配置
- C++ vector解析 (C++ 11)
- 10-04-2015 Recursion
- 互联网+
- 追求
- 安装体验开源车载系统Automotive Grade Linux
- Django中的URL配置和模板
- TinyXML的简单用法
- linux下Unity3d下如何使用动态链接库
- Generic Programming and the STL笔记1
- 《IT运维之道》之“做事完整性”解读
- 摘心
- html中文字长度超出宽度如何显示省略号“...”
- 协方差和它的c/c++算法
- PEP-8 规则