【问题标题】:Django render doesn't seem to pass dictionary to templateDjango 渲染似乎没有将字典传递给模板
【发布时间】:2015-04-19 08:52:21
【问题描述】:

更新:我通过卸载并重新安装 Python/Django 来修复它。不知道出了什么问题。

我正在尝试使用渲染将字典中的值打印到 html 模板(也尝试了 render_to_response),但它似乎不起作用。 HTML 模板无法识别我尝试传递给它的任何值。

Django 代码

def search(request):
    context = RequestContext(request)

    results2= [{'link':12, 'title':34, 'summary':56}, {'link':121, 'title':341, 'summary':561}]

    print results2  #is printed in command prompt, so this function is working
    return render_to_response('home.html', {'results2': results2}, context)
    #tried using render as well

HTML 模板

{% extends "base.html" %}
{% load staticfiles %}


    <form class="form-signin span8" id="user_form" method="post" action="">
        {% csrf_token %}
        <!-- Display the search form elements here -->
        <input type="text" size="50" name="query" value="" id="query" />
        <input class="btn btn-primary" type="submit" name="submit"    value="Search" placeholder="Put Search"/>
        <br />
    </form>        

    <p>{{results2}}</p>
    <p>test1</p>
    <p>{{link}}</p>
    <p>test2</p>

    <div style="clear: both;">
        <ol>
        {% for result in results2 %}
            <li>
                <strong><a href="{{ result.link }}">{{ result.title }}</a>                  </strong><br />
                <em>{{ result.summary }}</em>
            </li>
        {% endfor %}
        </ol>
    </div>

在服务器运行并调用函数时的命令提示符中,它正在打印“results2”,因此“搜索”函数肯定是有效的。但是,变量似乎没有被渲染。

查看渲染页面的源代码(相当于上面的):

    <form class="form-signin span8" id="user_form" method="post" action="">
        <input type='hidden' name='csrfmiddlewaretoken' value='Y4PfA0x8jH67jWUlTJfjbMzk0EAouPrp' />
        <!-- Display the search form elements here -->
        <input type="text" size="50" name="query" value="" id="query" />
        <input class="btn btn-primary" type="submit" name="submit" value="Search" placeholder="Put Search"/>
        <br />
    </form>

    <p></p>
    <p>test1</p>
    <p></p>
    <p>test2</p>


    <p>test2</p>
    <div style="clear: both;">
        <ol>

        </ol>
    </div>

所以字典列表'results2'没有被传递给模板。我没有收到任何错误。

【问题讨论】:

  • 这个数据结构的名字是“dictionary”,而不是“library”。
  • 如果你使用 django-debug-toolbar 你会得到一个侧边栏,你可以在其中检查传递给模板的实际上下文。
  • 这真的有用吗?扩展 base.html,然后在任何 {% block %} 之外添加代码? AFAIK 应该让代码不被渲染。检查您是否没有复制 base.html 并且
      也在 base.html 中。
  • @krs 不应该。必须添加块标签才能工作。 @SantoshGupta 请向我们展示您的 base.html。你应该有一个块标签,如:{% block content %}{% endblock %}
  • 或者,更好的是,现在完全去掉{% extends %} 标签。

标签: python html django templates render


【解决方案1】:

看起来您的模板希望 results2 是一个字典列表,而您传递的是一个字典。试试render_to_response('home.html', {'results2': [results2, ]}, context)

【讨论】:

  • 试过了。此外,将库更改为库列表“ results2= [{'link':12, 'title':34, 'summary':56}, {'link':121, 'title':341, 'summary': 561}]”。还是没有运气
【解决方案2】:

尝试像这样更改您的退货声明,

return render_to_response('home.html', {'results2': results2}, context_instance = context)

【讨论】:

    【解决方案3】:

    这种情况是否只发生在这个模板上,其他所有模板都可以正常工作吗?如果没有,请检查您的设置文件。确保您已定义 TEMPLATE_CONTEXT_PROCESSORS。它应该看起来像这样..

    from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP
    
    TEMPLATE_CONTEXT_PROCESSORS = TCP + (
    'django.core.context_processors.request',)
    

    除此之外,我没有看到代码有任何其他问题。

    【讨论】:

    • 我没有尝试其他模板,这是我第一次尝试渲染模板。
    • 奇怪..我没有运行代码,但 PyReda 运行了,它对他有用。所以,我看到的唯一原因是设置中的一些问题。仔细检查您的设置。另外,检查 TEMPLATE_DIRS 和 TEMPALTE_LOADERS 的值。
    【解决方案4】:

    你运行的是什么版本的 Django?如果是 1.8,上下文处理器被移动:

    在 Django 1.8 中更改:

    在 Django 1.8 中,内置模板上下文处理器已从 django.core.context_processors 移至 django.template.context_processors

    所以在你的设置文件中,TEMPLATE_CONTEXT_PROCESSORS 下应该是这样的:

    (
        "django.contrib.auth.context_processors.auth",
        "django.template.context_processors.debug",
        "django.template.context_processors.i18n",
        "django.template.context_processors.media",
        "django.template.context_processors.request",
        "django.template.context_processors.static",
        "django.template.context_processors.tz",
        "django.contrib.messages.context_processors.messages"
    )
    

    【讨论】:

    • 使用版本 1.7.1。我的 settings.py 中与上下文相关的唯一内容是“从 django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP TEMPLATE_CONTEXT_PROCESSORS = TCP + ('django.core.context_processors.request',) ”来自对此处另一条评论的建议
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 1970-01-01
    • 2012-03-02
    • 2014-05-12
    相关资源
    最近更新 更多