Django的templates模版

前面介绍的都是django.http.HttpResponse 把内容直接返回到网页上,这次介绍通过渲染模版的方法来显示内容

步骤:

  1、创建一个项目(略)

  2、创建一个app(略)

  3、添加项目到settings.INSTALLED_APPS (略)

  4、打开views.py写入一个试图

  from django.shortcuts import render

  def home(request):

    return render(request, 'index.html')

  在app项目下创建一个templates文件夹,里面新建一个index.html文件

  在index.html写入以下内容:

  

<!DOCTYPE html>
<html>
<head>
  <title>测试页面</title>
</head>
<body>
  测试内容
</body>
</html>

  5、将试图函数对应到urls.py网址中

  

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
  url(r'^$', 'mysite.mysite.index', name='index'),
  url(r'^admin/', include(admin.site.urls)),
]

  6、同步数据库

  python manage.py syncdb

  7、启动服务

  python manage.py runserver 0.0.0.0:80

  访问服务器就可以看到页面了

原文地址:https://www.cnblogs.com/chaoe/p/5983904.html