django 我想在index.html中显示'yyy',但在运行服务器时,页面显示正常,但没有'yyy'

eqoofvh9  于 2023-05-19  发布在  Go
关注(0)|答案(1)|浏览(96)

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

view.py

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

index.html

我想在index.html中显示'yyy'。但当我运行服务器时,页面显示正常,但'yyy'不可见。
当我跑步时:
python manage.py runserver
我没有看到任何错误。

7ajki6be

7ajki6be1#

试试这个
你需要从视图中将dict传入模板。
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>

相关问题