我想在 index.html 中显示'yyy'。但是当我运行服务器时,页面显示正常但'yyy'不显示
view.py
def index(request):
a1='yyy'
return render(request, "index.html", a1)
index.html
我想在 index.html 中显示“yyy”。但是当我运行服务器时,页面显示正常但没有显示“yyy”。我可以知道为什么吗?谢谢
django 蟒蛇
python manage.py runserver,成功。没有报错。我想知道为什么?
试试这个
你需要从视图中将字典传递给模板。
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>