1、添加URL模式来映射URL中的/xx/和新视图
修改view.py
from django.template import Template, Context
from django.http import HttpResponse
import datetime
def hello(request):
return HttpResponse("Hello world!")
def time(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
urls.py
from django.conf.urls import url
from django.contrib import admin
from firstDjango.view import hello,time
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello/$', hello), #添加路由接口
url(r'^time/$', time),
]
2、通过模版
def current_datetime(request):
now = datetime.datetime.now()
t = Template("<html><body>It is now {{ current_date }}.</body></html>")
html = t.render(Context({'current_date': now}))
return HttpResponse(html)
访问实现: