如何在任何模板上使用Django内置注释框架 [英] How to use Django build-in comment framework on any template
问题描述
models.py
class Entry(models.Model):
title = models.CharField(max_length = 250)
body = models.TextField()
pub_date = models.DateTimeField()
enable_comments = models.BooleanField()
urls.py
urlpatterns = patterns('',
url(r'^ profile /','django.views.generic.simple.direct_to_template',{
'template':'admin_ryu_blog /profile.html'},name='profile'),
)
现在我只是想在模板profile.html上使用评论框架。我该怎么办 ?您现在可以将profile.html视为空白页面。那么你可以添加任何你想要的代码,如果你可以得到一个注释表单显示与内置注释框架。
btw我已经尝试了下面的方法:
{%load comments%}
pre>
{%render_comment_form for profile%}
然后它会提示错误消息。对我以前的问题也是同样的回溯。
点击此处!解决方案你不能。评论框架期望一个对象引用。
但是,一个简单的解决方案是建立一个映射到如下URL的模型:
class CommentAnchor(models.Model):
path = models.CharField(max_length = 256)
构建上下文处理器构建这些对象并将它们添加到所有模板上下文中。记住将您的上下文处理器添加到您的
settings.TEMPLATE_CONTEXT_PROCESSORS
中,并记住在呈现模板时使用RequestContext
。def CommentAnchorProcessor(request):
comment_anchor,created = CommentAnchor.objects.get_or_create(path = request.path)
return {
'comment_anchor':comment_anchor,#now,这在每个模板都可用。
}
现在您可以通过这些新对象呈现评论。
{%render_comment_form for comment_anchor%}
I just begin to study Django build-in comments function. at first I think the comment template should work well on any page just with get_comment_form or render_comment_form .but now it really annoying when i add these code to a ordinary page. It doesn't work. maybe in a other word. I don't know how to specify the object to attached to when it come to a normal page. below is the detail message :
models.py class Entry(models.Model): title = models.CharField(max_length=250) body = models.TextField() pub_date = models.DateTimeField() enable_comments = models.BooleanField() urls.py urlpatterns = patterns('', url(r'^profile/','django.views.generic.simple.direct_to_template',{ 'template' : 'admin_ryu_blog/profile.html'},name='profile'), )
now i just want to using comment framework on template profile.html. what should i do ? you could regard profile.html as a blank page now. then you can add any code you want if you can get a comment form displayed with build-in comment framework.
btw I have tried the below method :
{% load comments %} {% render_comment_form for profile %}
then it prompt a error message. the same traceback on my previous question . click here!
解决方案You can't. The comments framework expects an object to reference.
But an easy solution that comes to mind is building a model that maps to URLs like so:
class CommentAnchor(models.Model): path = models.CharField(max_length=256)
Build a context processor that builds these objects and adds them to all template context . Remember to add your context processor to your
settings.TEMPLATE_CONTEXT_PROCESSORS
, and remember to useRequestContext
when rendering templates.def CommentAnchorProcessor(request): comment_anchor, created = CommentAnchor.objects.get_or_create(path=request.path) return { 'comment_anchor': comment_anchor, # now, this is available in every template. }
Now you can render comments via these new objects.
{% render_comment_form for comment_anchor %}
这篇关于如何在任何模板上使用Django内置注释框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!