您的位置:首页 > 编程语言 > Go语言

Django 学习笔记(三)模板导入

2017-08-06 23:46 603 查看

本章内容是将一个html网页放进模板中,并运行服务器将其展现出来。

平台:windows平台下Liunx子系统

目前的目录:

hello
├── manage.py
├── hello
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── HelloWorld
├── __init__.py
├── admin.py
├── apps.py
├── models.py ├── tests.py
└── views.py

我们只需要在HelloWorld中增加一个文件夹templates,把html网页放进这个文件夹内

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<title>welcome</title>
<meta charset="utf-8">
</head>
<body>
<h1> Hello World!</h1>
</body>
</html>

现在的,目录应该是:

hello
├── manage.py
├── hello
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── HelloWorld
├── templates
│   └──index.html
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
└── views.py

然后在views.py文件中,输入下面内容即可

from django.shortcuts import render

def index(request):
return render(request,'index.html')

最后Python manage.py runserver运行服务器,浏览器访问http://127.0.0.1:8000/index/即可访问模板文件

 

系列上一章:Django 学习笔记(二)第一个网页

系列下一章:Django 学习笔记(四)模板变量

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: