Django 1.1 - 评论 - “render_comment_form”返回模板语法错误
我想简单地使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了同样的问题,render_comment_form 模板标记触发了它。
问题肯定出在你的 URL 配置上,你的设置方式与我相同:
正确的方法是删除“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:
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.
错误消息表明它无法找到以下内容的反向网址:
因此基本上您的网址中没有正确配置某些内容。如果无法更多地了解事物的设置方式,就很难确切地知道是什么。
也许尝试重新排序 urls.py 中包含的 urls 模式,以强制 django 评论 url 到顶部?
The error message is indicated that it can't find a reverse url for:
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?
我今天也遇到了同样的问题。我正在引用 urls.py 中尚未创建的视图。
来自 http://docs.djangoproject.com/en/dev/主题/http/urls/#reverse
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
此错误表示它找到了视图
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