Python 如何在django应用程序中将字典值从视图传递到模板

Python 如何在django应用程序中将字典值从视图传递到模板,python,django,python-2.7,dictionary,Python,Django,Python 2.7,Dictionary,我只是想学习一个django项目。我已经使用django命令创建了project和project myapp内部。 在目录/myproject/myapp$中,我有views.py文件。另一个目录/myproject/myapp/templates$我有hello.html文件。我想使用render函数将字典值从views.py文件发送到hello.html文件。但是我接到了一个错误的电话 **Internal Server Error: /hello/ Traceback (most rece

我只是想学习一个django项目。我已经使用django命令创建了project和project myapp内部。
在目录/myproject/myapp$中,我有views.py文件。另一个目录/myproject/myapp/templates$我有hello.html文件。我想使用render函数将字典值从views.py文件发送到hello.html文件。但是我接到了一个错误的电话

**Internal Server Error: /hello/
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 42, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/joy/pythonWork/myproject/myapp/views.py", line 8, in hello
    name     : 'xyz',
NameError: global name 'name' is not defined**
我不知道如何解决这个问题。有人请帮帮我。 我的文件如下所示:

/views.py

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

# Create your views here.

def hello(request):
   personalDetails = {
       name     : 'xyz',
       phoneno  : '722924374'
   }
   return render(request, "hello.html", {personalDetails})
/hello.html

<html>
<body>

<h1>Hello</h1>{{personalDetails}}

</body>
</html>

你好{{personalDetails}

您应该像这样使用
渲染

return render(request, "hello.html", {"personalDetails": personalDetails})
试试这个变化

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

# Create your views here.

def hello(request):
   personalDetails = {
       name     : 'xyz',
       phoneno  : '722924374'
   }
   return render(request, "hello.html", **personalDetails)
在你的html中

<html>
<body>

<h1>Hello</h1>{{name}}

</body>
</html>

你好{{name}
在模板中:

<html>
<body>
{% for item in personalDetails %}
   <h1>Hello</h1>{{item.name}}
{% endfor %}

</body>
</html>

{%用于personalDetails%中的项目}
你好{{item.name}
{%endfor%}

您正在传递一本词典。字典键应该是字符串

def hello(request):
   personal_details = {
       'name': 'xyz',
       'phoneno': '7022924374',
   }
   return render(request, 'hello.html', {'personal_details': personal_details})

在浏览器中:未定义/hello/global name“name”处的名称错误请求方法:GETtry如上所述更改,或者如果您正在模板中传递dict,则使用dict查找筛选器从dict浏览器中获取值:未定义/hello/global name“name”处的名称错误请求方法:GET…Dis you fix
{personalDetails}
{{personalDeatils.name}}
在浏览器中:name错误在/hello/global name'name'未定义请求方法:GET…python字典中的键必须是字符串。返回render(请求,“hello.html”,“personal\u details”:personal\u details})^IndentationError:未缩进与任何外部缩进级别不匹配
def hello(request):
   personal_details = {
       'name': 'xyz',
       'phoneno': '7022924374',
   }
   return render(request, 'hello.html', {'personal_details': personal_details})