Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django变量赢了';不能在模板中渲染_Python_Django_Django Templates_Django Views - Fatal编程技术网

Python Django变量赢了';不能在模板中渲染

Python Django变量赢了';不能在模板中渲染,python,django,django-templates,django-views,Python,Django,Django Templates,Django Views,Django版本2.1.3 Python版本3.7 编写示例代码只是为了理解Django。现在我使用的是模板,在渲染变量方面,我运气不佳 在views.py文件夹中,我创建了一个小字典,并在变量content from django.shortcuts import render # Create your views here. posts = [ { 'Title': 'Challanger 1', 'Name': 'Sammy', 'Age': '33'

Django版本2.1.3

Python版本3.7

编写示例代码只是为了理解Django。现在我使用的是模板,在渲染变量方面,我运气不佳

views.py
文件夹中,我创建了一个小字典,并在变量content

from django.shortcuts import render

# Create your views here.

posts = [
    {
    'Title': 'Challanger 1',
    'Name': 'Sammy',
    'Age': '33',
    'Food': 'Seafood'
    },   
    {
    'Title': 'Challanger 2',
    'Name': 'Sammy',
    'Age': '33',
    'Food': 'Seafood'
    }
]

def home(request):
    content = {
        'posts': posts
    }
    return render(request, 'blog/home.html', content)
在我的
home.html
文件中,我在
{{variable}
旁边添加了一些123,以确保
.html
文件连接到
view.py
。当I
py manage.py runserver
时,仅显示123,但我的
{{variables}}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

    {% for post in posts %}

        <p>123 {{ post.name }}</p>
        <h1>123  {{ post.title }}</h1>
        <h1>123 {{ post.age }}</h1>

    {% endfor %}

</body>
</html>
从浏览器打开“查看源代码”时:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>



        <p>123 </p>
        <h1>123  </h1>
        <h1>123 </h1>



        <p>123 </p>
        <h1>123  </h1>
        <h1>123 </h1>



</body>
</html>

文件
123

123 123 123

123 123
旁注:当我在
home.html
文件中时,我注意到只有
{post.title}
{post.name}
自动填充和
{post.age}
{post.food}
从不自动填充


有时,我删除了
home.html
文件中的所有内容,重写代码时没有任何
{{variables}
自动填充。无论哪种方式,最终结果都是一样的,变量不会加载

您在模板中使用了
post.name
(全部小写),但是您在python代码中定义了字典,将
name
(大写N)作为键


尝试使用
{{post.Name}

问题在于,字典键区分大小写。 请尝试以下代码:

 <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>

        {% for post in posts %}

            <p>123 {{ post.Name }}</p>
            <h1>123  {{ post.Title }}</h1>
            <h1>123 {{ post.Age }}</h1>

        {% endfor %}

    </body>
    </html>

文件
{posts%%中的post为%s}
123{{post.Name}

123{{post.Title} 123{{post.Age} {%endfor%}
您提到了姓名、标题、年龄,但在html中添加了姓名、标题、年龄,这是一个错误。非常感谢你。
 <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>

        {% for post in posts %}

            <p>123 {{ post.Name }}</p>
            <h1>123  {{ post.Title }}</h1>
            <h1>123 {{ post.Age }}</h1>

        {% endfor %}

    </body>
    </html>