Django网站未显示字典值

发布于 2025-02-08 06:10:06 字数 586 浏览 0 评论 0原文

我正在为Django使用VS代码。我不确定为什么,但是默认的Django网站没有显示预期的输出。输出忽略了字典值。

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
   context = {
       'name': 'Patrick',
       'age' : 23,
       'nationality':'British',

   }
   return render(request, 'index.html', context)
<h1>
    Welcome, {{name}}<br> You are {{age}} years old.
</h1>

(在服务器网站中输出)

Welcome,
You are years old.

I am using VS code for django. I am not sure why but the default django website is not showing the expected output. The output is ignoring the dictionary values.

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
   context = {
       'name': 'Patrick',
       'age' : 23,
       'nationality':'British',

   }
   return render(request, 'index.html', context)
<h1>
    Welcome, {{name}}<br> You are {{age}} years old.
</h1>

(output in server website)

Welcome,
You are years old.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素罗衫 2025-02-15 06:10:06

您的代码正确,但可能会导致预期输出不显示两件事。

  1. 尝试在VSCODE上保存代码,也许您没有保存代码。
  2. 尝试突破服务器并重新运行。

Your code is correct but there might be two things that might cause the expected output not to show.

  1. Try saving your code on Vscode maybe you have not saved your code.
  2. Try breaking out of the server and re-run it again.
荒路情人 2025-02-15 06:10:06

尝试以这种方式发送上下文:

def index(request):
   context = {
       'name': 'Patrick',
       'age' : 23,
       'nationality':'British',

   }
   return render(request, 'index.html', {
    'context':context
   })

您的H​​TML文件应该是这样的:

<h1>
   Welcome, {{context.name}}<br> You are {{context.age}} years old.
</h1>

try sending the context this way :

def index(request):
   context = {
       'name': 'Patrick',
       'age' : 23,
       'nationality':'British',

   }
   return render(request, 'index.html', {
    'context':context
   })

your HTML file should be something like this:

<h1>
   Welcome, {{context.name}}<br> You are {{context.age}} years old.
</h1>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文