【问题标题】:context variable blank when passed to template传递给模板时上下文变量为空
【发布时间】:2014-04-14 03:42:03
【问题描述】:

上下文变量在模板中显示时为空。我已经在 Django Shell 中测试了查询,查询正在返回 Shell 中的数据。

views.py (https://github.com/josieh/playground-finder/blob/master/playgroundApp/views.py#L11-L26)

def Playground_List(request):

    context = {
        'playgrounds': Playground.object.all()
    }
    return render (request, "home.html", context)

模板 home.html (https://github.com/josieh/playground-finder/blob/master/playgroundApp/templates/playgroundApp/home.html#L48-L54)

 {% for playground in playgrounds %}
        {# Each "player" is a player model object. #}
            <tr>
                <td> {{ playground.Name }}</td>
                <td> {{ playground.Address }}</td>
            </tr>
 {% endfor %}

你能帮忙解释一下为什么游乐场的上下文变量会是空的吗? 感谢您的帮助!

【问题讨论】:

    标签: python django


    【解决方案1】:

    一个问题是它应该是Playground.objects.all()。我想这可能是问题文本中的转录错误,因为如果它实际上是您在程序中编写的内容,那应该会抛出 AttributeError

    您确定Playground 模型的属性称为NameAddress,而不是nameaddress?将属性名称大写是违反 Python 风格约定的。如果您错误地将这些名称大写,那么这确实会导致您的输出显示为空白。

    print 语句/函数(取决于 Python 2 或 3)注入您的视图以打印上下文和/或 QuerySet 可能会帮助您调试问题。

    【讨论】:

    • 谢谢!这些是非常有用的建议,让我们朝着正确的方向前进!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 2017-11-10
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    相关资源
    最近更新 更多