美文网首页
django学习2

django学习2

作者: Retree | 来源:发表于2018-08-14 16:40 被阅读0次

创建模板

在manage.py的同级目录创建templates文件夹
在templates目录下创建hello.html

编辑模板

<h1>{{hello}}</h1>

修改study/setting.py

修改 TEMPLATES 中的 DIRS 为 [BASE_DIR+"/templates",]

修改view.py

增加一个新的对象,用于向模板提交数据
数据与视图分离
这里使用 render 来替代之前使用的 HttpResponse。render 还使用了一个字典 context 作为参数。context 字典中元素的键值 "hello" 对应了模板中的变量 "{{ hello }}"。

# -*- coding: utf-8 -*-
 
#from django.http import HttpResponse
from django.shortcuts import render
 
def hello(request):
    context          = {}
    context['hello'] = 'Hello World master!'
    return render(request, 'hello.html', context)

如果服务还没关,刷新页面

image.png

相关文章

网友评论

      本文标题:django学习2

      本文链接:https://www.haomeiwen.com/subject/kfuvbftx.html