简单记录一下django的评论框架comments和一些问题

官方介绍:

Django’s comments framework

Example of using the built-in comments app

 

To get started using the comments app, follow these steps:

  • Install the comments framework by adding ‘django.contrib.comments’ to INSTALLED_APPS.
  • Run manage.py syncdb so that Django will create the comment tables.
  • Add the comment app’s URLs to your project’s urls.py:
  • Use the comment template tags below to embed comments in your templates.

首先需要导入自定义标签。

{% load comments %}

 

Once loaded you can use the template tags below.

 

在第一个例子get_comment_count中:

{% get_comment_count for entry as comment_count %}.

In the above, blog.entry is the app label and (lower-cased) model name of the model class.

 

另外各种tags就直接看Django’s comments framework就行了。

 

在使用过程中,在提交评论时总是提示这个问题:

CSRF verification failed. Request aborted

提示:

In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django’s CSRF mechanism has not been used correctly. For POST forms, you need to ensure:

    Your browser is accepting cookies.
    The view function uses RequestContext for the template, instead of Context.
    In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
    If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.

 

在网上看到的基本都是这种解答(传送门):

 

1、在表单Form里加上{% csrf_token %}

2、在Settings里的MIDDLEWARE_CLASSES增加配置:(一般默认就有)

‘django.middleware.csrf.CsrfViewMiddleware’,

#’django.middleware.csrf.CsrfResponseMiddleware’,

 

估计1.4的版本有了变化,在网上查了半天,最后还是在官方document上找到了:

传送门

它是这么处理的:

 

from django.core.context_processors import csrf
from django.shortcuts import render_to_response

def my_view(request):
    c = {}
    c.update(csrf(request))
    # ... view code here
    return render_to_response("a_template.html", c)
 
 

另外关于使用自带的评论栏和自定义评论栏可以参考这位朋友的:

Django学习笔记—Comments库的使用方法小记

发布者

Tanky Woo

Tanky Woo,[个人主页:https://tankywoo.com] / [新博客:https://blog.tankywoo.com]

《简单记录一下django的评论框架comments和一些问题》有4个想法

  1. 使用render_comment_form提交表单时也会出现CSRF verification failed. Request aborted这样的问题。我想问一下,自己如果不写view,难道没有默认的view来处理提交的表单么?

发表评论

电子邮件地址不会被公开。 必填项已用*标注