django - 无法在 django 中使用变量
问题描述
我最近开始了我的 django 在线课程,但遇到了一些问题。我无法使用从 index.html 传递到 about.html 的变量。但在 about.html 中没有显示出来。
.py 文件代码:
from django .http import HttpResponse
from django.shortcuts import render
def index(request):
return render(request , 'index.html')
def about(request):
t1 = print(request.GET.get('text' , 'default'))
return render(request , 'about.html' , t1)
index.html 文件代码:
<!DOCTYPE html>
<html>
<head>
<title>template</title>
</head>
<body>
<h1> hello everyone </h1>
<form action="/about" , method="get">
<textarea name="text" style="margin: 0px; width: 1245px; height: 171px;"></textarea>
<input type="submit" name="OK">
</form>
</body>
</html>
about.html 文件代码:
<!DOCTYPE html>
<html>
<head>
<title>template</title>
</head>
<body>
<h1>you typed {{t1}}</h1>
</form>
</body>
</html>
解决方案
print(..)
不返回任何东西。您可以将变量传递给上下文,例如:
def about(request):
return render(
request ,
'about.html' ,
{'t1': request.GET.get('text' , 'default')}
)
推荐阅读
- android - 如何从适配器更新日期到 recyclerView?
- ios - 有没有办法在 iOS 13 和 iOS14 上使用 Float16
- jquery - 通过追加添加新元素时选项卡不起作用
- python - 我的 Django 教程出现 NoReverseMatch 问题
- javascript - 无法编辑具有动态命名属性的 JavaScript 对象:对象文字中缺少属性
- typescript - 如何防止get操作中的嵌套订阅?
- javascript - 下拉元素的 useState - ReactJS
- python - Python pandas DataFrame:检查n个元素是否具有连续值?
- reactjs - API调用数据库后上下文不更新
- r - 像 RStudio 一样缩进 VSCode