我正在使用django-评语,我想知道如何禁止匿名用户发布评论。
仅仅不向匿名用户显示表单是不够的,因为匿名用户仍然可以将请求发送到正确的URL。可以从以前的响应中计算出CSRF令牌和security_hash令牌,所以这还不够。
发布于 2016-01-06 20:57:39
如果您的目标是不允许匿名用户发布评论,则可以尝试覆盖默认提供的urls。将login_required添加到post_comment视图中。通过修改django项目的urls.py来做到这一点:
from django.conf.urls import url, include
from django.contrib.auth.decorators import login_required
from django_comments.views.comments import post_comment
urlpatterns = [
...
# the line below will override the url supplied in django_comments.urls
url(r'^comments/post/$', login_required(post_comment), name='comments-post-comment'),
url(r'^comments/', include('django_comments.urls')),
...
]每当向/comments/post/发出请求时,它都将使用视图的login_required版本,因为模式将在django_comments.urls中的模式之前匹配。
https://stackoverflow.com/questions/34638085
复制相似问题