django学习2

创建模板

在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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容