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

huangapple go评论76阅读模式
英文:

I want to display 'yyy' in the index.html, but when running the server, the page displays normally but without the 'yyy'

问题

I want to display 'yyy' in the index.html, but when I run the server, the page is displayed normally, but 'yyy' is not visible.

view.py

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

index.html

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

I want to display 'yyy' in the index.html, but when I run the server, the page is displayed normally, but 'yyy' is not visible.

When I run:

python manage.py runserver

I see no errors.

英文:

i want to display 'yyy' in the index.html, but when I run the server , the page is displayed normally but 'yyy' is not visible.

view.py

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

index.html

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

I want to display 'yyy' in the index.html. but when I run the server, the page displayed normally but 'yyy' is not visible.

When I run:

python manage.py runserver

I see no errors.

答案1

得分: 1

尝试这个

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

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>
英文:

try this

you need pass dict into template from view.

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

def index(request):
   context = {&#39;a1&#39;: &#39;yyy&#39;}
   return render(request, &quot;index.html&quot;, context)

in template

&lt;h1&gt;{{a1}}&lt;/h1&gt;

huangapple
  • 本文由 发表于 2023年5月11日 14:09:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定