我想在index.html中显示'yyy'。但是当我运行服务器时,页面显示正常但'yyy'不显示。我可以知道为什么吗?谢谢

问题描述 投票:0回答:1

我想在 index.html 中显示'yyy'。但是当我运行服务器时,页面显示正常但'yyy'不显示

view.py

def index(request):
   a1='yyy'
   return render(request, "index.html", a1)

index.html

我想在 index.html 中显示“yyy”。但是当我运行服务器时,页面显示正常但没有显示“yyy”。我可以知道为什么吗?谢谢enter image description here

django 蟒蛇

python manage.py runserver,成功。没有报错。我想知道为什么?

python html django view
1个回答
0
投票

试试这个

你需要从视图中将字典传递给模板。

https://docs.djangoproject.com/en/4.2/ref/templates/api/#django.template.Context

def index(request):
   context = {'a1': 'yyy'}
   return render(request, "index.html", context)

在模板中

<h1>{{a1}}</h1>
© www.soinside.com 2019 - 2024. All rights reserved.