Django 1.1 - 评论 - “render_comment_form”返回模板语法错误

发布于 2024-08-14 18:33:29 字数 2103 浏览 6 评论 0原文

我想简单地使用 Django 的内置注释模块在模板中呈现内置注释表单,但这会返回 TemplateSyntaxError 异常。

我需要帮助调试此错误,因为在谷歌搜索并使用 Django API 参考之后,我仍然没有进一步了解。

信息:

这是模板 '_post.html'[shortened]:

<div id="post_{{ object.id }}">
<h2>
    <a href="{% url post object.id %}">{{ object.title }}</a>
    <small>{{ object.pub_date|timesince }} ago</small>
    </h2>
    {{ object.body }}
    {% load comments %}
    {% get_comment_count for object as comment_count %}
    <p>{{ comment_count }}</p>
    <!-- Returns 0, because no comments available  -->
    {% render_comment_form for object %}
    <!-- Returns TemplateSyntaxError -->

这是渲染时的异常输出:

Caught an exception while rendering: Reverse for 'django.contrib.comments.views.comments.post_comment'
with arguments '()' and keyword arguments '{}' not found.1  
{% load comments i18n %}
        <form action="{% comment_form_target %}" method="post">
          {% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
          {% for field in form %}
            {% if field.is_hidden %}
              {{ field }}
            {% else %}
          {% if field.errors %}{{ field.errors }}{% endif %}
          <p
            {% if field.errors %} class="error"{% endif %}
            {% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}>
            {{ field.label_tag }} {{ field }}

/posts/urls.py[shortened]:

queryset = {'queryset': Post.objects.all(),
            'extra_context' : {"tags" : get_tags}
           }   
urlpatterns = patterns('django.views.generic.list_detail',
    url('^$',                           'object_list',      queryset,
        name='posts'),
    url('^blog/(?P<object_id>\d+)/$',   'object_detail',    queryset,
        name='post'),
)

/urls.py[shortened]:

urlpatterns = patterns('',
    (r'', include('posts.urls')),
    (r'^comments/$', include('django.contrib.comments.urls')),
)

I want to simply render a built-in comment form in a template, using Django's builtin commenting module, but this returns a TemplateSyntaxError Exception.

I need help debugging this error, please, because after googling and using the Django API reference, I'm still not getting any farther.

Info:

This is the template '_post.html'[shortened]:

<div id="post_{{ object.id }}">
<h2>
    <a href="{% url post object.id %}">{{ object.title }}</a>
    <small>{{ object.pub_date|timesince }} ago</small>
    </h2>
    {{ object.body }}
    {% load comments %}
    {% get_comment_count for object as comment_count %}
    <p>{{ comment_count }}</p>
    <!-- Returns 0, because no comments available  -->
    {% render_comment_form for object %}
    <!-- Returns TemplateSyntaxError -->

This is the Exception output, when rendering:

Caught an exception while rendering: Reverse for 'django.contrib.comments.views.comments.post_comment'
with arguments '()' and keyword arguments '{}' not found.1  
{% load comments i18n %}
        <form action="{% comment_form_target %}" method="post">
          {% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
          {% for field in form %}
            {% if field.is_hidden %}
              {{ field }}
            {% else %}
          {% if field.errors %}{{ field.errors }}{% endif %}
          <p
            {% if field.errors %} class="error"{% endif %}
            {% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}>
            {{ field.label_tag }} {{ field }}

/posts/urls.py[shortened]:

queryset = {'queryset': Post.objects.all(),
            'extra_context' : {"tags" : get_tags}
           }   
urlpatterns = patterns('django.views.generic.list_detail',
    url('^

/urls.py[shortened]:

urlpatterns = patterns('',
    (r'', include('posts.urls')),
    (r'^comments/
,                           'object_list',      queryset,
        name='posts'),
    url('^blog/(?P<object_id>\d+)/

/urls.py[shortened]:


,   'object_detail',    queryset,
        name='post'),
)

/urls.py[shortened]:


, include('django.contrib.comments.urls')),
)
, 'object_list', queryset, name='posts'), url('^blog/(?P<object_id>\d+)/

/urls.py[shortened]:

, 'object_detail', queryset, name='post'), )

/urls.py[shortened]:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

奢华的一滴泪 2024-08-21 18:33:29

我遇到了同样的问题,render_comment_form 模板标记触发了它。

问题肯定出在你的 URL 配置上,你的设置方式与我相同:

(r'^comments/

正确的方法是删除“comments/”后面的“$”:

(r'^comments/', include('django.contrib.comments.urls'))

否则 django 无法正确包含所有内容路径 comments/... 下的必要网址

希望这有帮助。

, include('django.contrib.comments.urls'))

正确的方法是删除“comments/”后面的“$”:

否则 django 无法正确包含所有内容路径 comments/... 下的必要网址

希望这有帮助。

I had the same exact problem, render_comment_form template tag was triggering it.

The issue is certainly with your URL config, you had it set the same way i did:

(r'^comments/

The correct way is to remove the '$' after 'comments/':

(r'^comments/', include('django.contrib.comments.urls'))

Otherwise django can't properly include all necessary urls under the path comments/...

Hope this helps.

, include('django.contrib.comments.urls'))

The correct way is to remove the '$' after 'comments/':

Otherwise django can't properly include all necessary urls under the path comments/...

Hope this helps.

过期以后 2024-08-21 18:33:29

错误消息表明它无法找到以下内容的反向网址:


   django.contrib.comments.views.comments.post_comment

因此基本上您的网址中没有正确配置某些内容。如果无法更多地了解事物的设置方式,就很难确切地知道是什么。

也许尝试重新排序 urls.py 中包含的 urls 模式,以强制 django 评论 url 到顶部?

The error message is indicated that it can't find a reverse url for:


   django.contrib.comments.views.comments.post_comment

So basically something isn't configured right in your urls. Without being able to see more of how things are setup it's difficult to know exactly what.

Maybe try re-ordering the urls pattern includes in your urls.py, to force the django comments urls to the top?

何时共饮酒 2024-08-21 18:33:29

我今天也遇到了同样的问题。我正在引用 urls.py 中尚未创建的视图。

来自 http://docs.djangoproject.com/en/dev/主题/http/urls/#reverse

作为确定 URL 名称的一部分
映射到哪些模式,reverse()
函数必须导入你的所有
URLconf 文件并检查名称
每个视图。这涉及到导入
每个视图函数。如果有的话
导入您的任何内容时出错
查看函数,它会导致
reverse() 会引发错误,即使
该视图函数不是您的函数
正在努力扭转局面。

确保您引用的任何视图
在你的 URLconf 文件中存在并且可以
正确导入。不包括
引用了您尚未引用的视图的行
还没有写,因为这些观点会
不可导入。

I had this same problem today. I was referencing a view in urls.py that I hadn't created yet.

From http://docs.djangoproject.com/en/dev/topics/http/urls/#reverse

As part of working out which URL names
map to which patterns, the reverse()
function has to import all of your
URLconf files and examine the name of
each view. This involves importing
each view function. If there are any
errors whilst importing any of your
view functions, it will cause
reverse() to raise an error, even if
that view function is not the one you
are trying to reverse.

Make sure that any views you reference
in your URLconf files exist and can be
imported correctly. Do not include
lines that reference views you haven't
written yet, because those views will
not be importable.

谁的年少不轻狂 2024-08-21 18:33:29

此错误表示它找到了视图 django.contrib.comments.views.comments.post_comment

但没有传递 args () 或 kwargs{}。

它没有将 object.id 的值传递到 url 中。

取出 url 标签,查看

的 id 是否反映了正确的 object.id

This error is saying that it found the view django.contrib.comments.views.comments.post_comment

but no args () or kwargs{} were passed.

Its not passing a value for object.id into the url.

Take out the url tag and see if the id of the <div id="post_{{object.id}}"> reflects a proper object.id

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文