How to use Django build-in comment framework on any template

北战南征 提交于 2019-12-13 03:06:30

问题


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!


回答1:


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 use RequestContext 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 %} 


来源:https://stackoverflow.com/questions/14515384/how-to-use-django-build-in-comment-framework-on-any-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!