django之创建第7个项目-url配置
Posted Xiao|Deng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django之创建第7个项目-url配置相关的知识,希望对你有一定的参考价值。
1、配置urls.py
from django.conf.urls import patterns, include, url #Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns(‘‘, # Uncomment the admin/doc line below to enable admin documentation: url(r‘^admin/doc/‘, include(‘django.contrib.admindocs.urls‘)), # Uncomment the next line to enable the admin: url(r‘^admin/‘, include(admin.site.urls)), #url(r‘^$‘, ‘blog.views.index‘), url(r‘^blog/index/$‘, ‘blog.views.index‘), url(r‘^blog/time/$‘, ‘blog.views.time‘), )
2、配置views.py
# Create your views here. #coding:utf-8 from django.http import HttpResponse import datetime #导入templates文件所需导入库 from django.template import loader,Context def time(request): t = loader.get_template("time.html") user = {"today": datetime.datetime.now()} c = Context(user) return HttpResponse(t.render(c)) def index(request): #加载器,加载模板 t=loader.get_template("index.html") # django之创建第4-1个项目-Dict形式 user = {"name": "xiaodeng", "sex": "male", "age": 28,"today":datetime.datetime.now()} c = Context({"test": user}) # 在这里test位变量,user为变量的值 return HttpResponse(t.render(c))
3、创建time.html文件并写入下面的内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>time</title> </head> <body> <li>当前时间:{{today}}</li> </body> </html>
4、百度云盘:http://pan.baidu.com/s/1qYtcap2
以上是关于django之创建第7个项目-url配置的主要内容,如果未能解决你的问题,请参考以下文章
django之创建第7-4个项目-配置views文件实现url传值