【问题标题】:Django cant read variablesDjango 无法读取变量
【发布时间】:2014-10-03 18:35:32
【问题描述】:

我对 django 有疑问 在我的views.py中我有这个

def home(request):
    template = "index.html"
    test = "hello"
    Settings = settings.objects.all()
    return render_to_response(template ,{"Settings" : settings}, context_instance=RequestContext(request))

在我的 index.html 中

{{test}}

但它没有返回任何东西 如果我写的话,我的模型还有一个名为 logo_img 的字段

{{ Settings.logo_img }}

也不行,有什么想法吗?

【问题讨论】:

    标签: python django model processors


    【解决方案1】:

    用“hello”渲染测试变量:

    def home(request):
        template = "index.html"
        test = "hello"
        return render_to_response(template, {"test": test})
    

    只要您的模板目录配置正确,并且找到了 index.html,这应该可以工作。 我不确定您将变量“设置”用于什么目的。分配后它不会被引用。

    【讨论】:

    • 它起作用了:D thz,我还有一个问题,有没有办法返回设置模型但不使用 {% for %} ?
    猜你喜欢
    • 2019-11-21
    • 2020-08-02
    • 2017-03-20
    • 2014-08-16
    • 2020-06-04
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    相关资源
    最近更新 更多