第一个template
templates/test.html简单第一个 template html文件。
<!--在test.html文件夹下添加-->
<!DOCTYPE html>
<html>
<head>
<title>Just test template/title>
<style>
body {
background-color: red;
}
strong {
color: LightSeaGreen;
}
</style>
</head>
<body>
<h1>Hello World!/h1>
<strong>/strong>
</body>
</html>
其中``是Django Template中变量的表示方式。
在article/view.py中添加一个函数逻辑。
from django.shortcuts import render
from django.http import HttpResponse
from article.models import Article
from datetime import datetime
# Create your views here.
def home(request):
return HttpResponse("Hello World, Django")
def detail(request, my_args):
post = Article.objects.all()[int(my_args)]
str = ("title = %s, category = %s, date_time = %s, content = %s"
% (post.title, post.category, post.date_time, post.content))
return HttpResponse(str)
def test(request) :
return render(request, 'test.html', {'current_time': datetime.now()})
render()函数中第一个参数是request 对象, 第二个参数是一个模板名称,第三个是一个字典类型的可选参数. 它将返回一个包含有给定模板根据给定的上下文渲染结果的 HttpResponse对象。
然后设置对应的url在my_blog/urls.py下
url(r'^test/$', 'article.views.test'),
重新启动服务器python manage.py runserver, 然后在浏览器中输入http://127.0.0.1:8000/test/, 可以看到