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

原文地址:https://www.cnblogs.com/dengyg200891/p/5353583.html