Django模板上下文变量不显示

时间:2018-08-21 15:38:45

标签: django django-templates django-context

我有一个自定义的article.html模板,该模板将覆盖aldryn newsblog插件随附的默认article.html模板。在此模板中,我有2行包含一些自定义功能:

{% include 'comment_form.html' %}
{% include 'list_comments.html' %}

以下是这两个模板文件的详细信息:

list_comments.html模板:

{% load cms_tags staticfiles sekizai_tags %}
{% if comments %}
    {% for item in comments %}
    <div class="comment paragraph">
        <h4>{{ item.author }}</h4>
        <p>{{ item.comment }}</p>
        <p>{{ item.date }}</p>
    </div>
    {% endfor %}
{% else %}
    <p>No comments exist on this blog. Be the first to comment!</p>
{% endif %}
{{ another }}
{{ thisdoesntexist }}

和comment_form.html

{% load cms_tags staticfiles sekizai_tags %}
<div id="comment_form">
<div class="container constrained paragraph">
    <h5>Submit a comment</h5>
    <form method="post">
        {% csrf_token %}

        {{ comment_form }}

        <input type="hidden" name="page" value="{{ article.id }}">

        <input type="submit" value="Submit Comment">
    </form>
</div>

and views.py我有2个函数来返回带有上下文变量的视图:

def display_form(request):
    comment_form = CommentForm()

    return render(request, 'comment_form.html', {'comment_form': comment_form})


def get_blog_comments(request):
    qs = BlogComment.objects.all()
    context = {'comments': qs, 'another': 'TEST STRING'}
    return render(request, 'list_comments.html', context)

在两个模板中,上下文变量均不输出任何内容。我因自己做错了事而茫然。我几乎直接从Django表单教程中复制了代码。 django.template.context_processors.request包含在我的settings.py context_processors中。

我不确定我的{{thisdoesntexist}}变量是否会引发错误,但不是。模板显示除上下文变量以外的所有其他html,因此这不是模板链接问题。

编辑:

urls.py

urlpatterns = [
url(r'^filer/', include('filer.urls')),
url(r'^location/', include('locations.urls')),
url(r'^captcha/', include('captcha.urls')),
url(r'^', include('cms.urls')),
]

1 个答案:

答案 0 :(得分:0)

未显示上下文变量,因为您没有传入任何一个include语句。

{% include 'comment_form.html' with comment_form=comment_form %}
{% include 'list_comments.html' with comments=comments %}