线程评论 - csrf 令牌和用户名

发布于 2024-10-14 17:16:12 字数 701 浏览 10 评论 0原文

我正在使用 django-threadedcomments。除了两件事外,一切正常:csrf 令牌和用户模板标签。

问题是,当用户提交评论时,表单没有 csrf 令牌,因此无法在服务器端验证表单。尝试将 csrf 令牌添加到线程注释通过内部传递的字典中,但没有结果;不断收到错误(其中大多数错误表明此方法只需要 2 个参数,其中给出了 3 个参数)。尝试修复这些方法以接受 3 个参数并进一步传递第三个参数;没有成功。

过去有人偶然发现过同样的问题并解决了吗?因为这对我来说不是一个可接受的解决方案:

MIDDLEWARE_CLASSES = (
    #'django.middleware.csrf.CsrfViewMiddleware',
)

第二个 - 有一个 HTML 帮助器来获取发表评论的用户的 user_id 。是否有一个开箱即用的 html 帮助器可以通过 id 获取用户的名称,或者我必须自己编写它?

http://code.google.com/p/django-threadedcomments/

这里是该项目的代码,我无法确切地告诉它的哪些部分应该发布在这里,所以我只是给出整个项目的链接。

我真的被困在这里,欢迎任何帮助。

提前致谢。

I am using django-threadedcomments. Everything works fine except 2 things: csrf token and user template tag.

Problem is, when user submits a comment, there is no csrf token for the form, so the form could not be validated server-side. Tried adding csrf token to the dictionaries that threaded-comments passes internal with no result; kept receiving errors (most of them telling that this-method takes only 2 arguments with 3 given). Tried to fix those methods to accept 3 arguments and just pass third one further; no success.

Did someone stumble upon the same problem in past and solved it? because this is not an acceptable solution for me:

MIDDLEWARE_CLASSES = (
    #'django.middleware.csrf.CsrfViewMiddleware',
)

Second one - there is a HTML helper to get the user_id for the user who posted a comment. Is there an out of the box html helper to get the name of the user by id or would i have to write it myself?

http://code.google.com/p/django-threadedcomments/

Here is the code for the project, I cant really tell exactly which chunks of it should be posted here so I just give link to the entire project.

I am really stuck in here and any help would be welcomed.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栖竹 2024-10-21 17:16:12

尝试将 csrf 令牌添加到
串联评论的词典
通过内部但没有结果;

csrf_token 是一个模板标记——它不应该作为参数传递到某个地方。

我查看了 threadedcomments,它基于 contrib.comments,没有 html 渲染,因此您可以将 csrf_token 插入您的模板。

显示表单代码的模板代码是什么样的?

如果您启用了 CsrfViewMiddleware 并且在视图中使用 RequestContext,则只需在 中添加 {% csrf_token %} >

标签。

关于获取用户名
ThreadedCommentComment 的子类,它具有 name 属性,或者您可以直接访问 User 。 。

{% for comment in comments % 
    {{ comment.user.first_name }}
    {{ comment.name }}
{% endfor %}

Tried adding csrf token to the
dictionaries that threaded-comments
passes internal with no result;

csrf_token is a template tag -- it shouldn't be passed as an argument somewhere.

I took a look at threadedcomments and it's based on contrib.comments with no html rendering, so it's up to you to insert the csrf_token in your template.

What does your TEMPLATE code look like that is displaying your form code?

If you have CsrfViewMiddleware enabled and you are using RequestContext in your view, you simply need to add {% csrf_token %} inside of your <form></form> tags.

As for getting the user name:
ThreadedComment is a subclasses of Comment which has a name property, or you could just access the User directly...

{% for comment in comments % 
    {{ comment.user.first_name }}
    {{ comment.name }}
{% endfor %}
何以心动 2024-10-21 17:16:12

您应该在视图中使用 {% csrf_token %} 标签或 @csrf_protect

You should use {% csrf_token %} tag or @csrf_protect in a views

未央 2024-10-21 17:16:12

您可以将表单放入其自己的模板中,并将其 {% include %} 放入您的页面模板中。从 Django 1.3 开始,{% include %} 可以将上下文变量传递给包含的模板。以下是我使用 django.contrib.comments 而不是 templatetag 的内容:

...
{% include "comments/comment-form.html" with content_object=article user=request.user %}
...

{%csrf_token %} 在此包含的模板中工作,因为它使用您的主视图上下文。

You can put your form in its own template and {% include %} it into your page template. As of Django 1.3, {% include %} can pass context variables to the included template. Here's what I'm using with django.contrib.comments instead of a templatetag:

...
{% include "comments/comment-form.html" with content_object=article user=request.user %}
...

{%csrf_token %} works in this included template because it's using your main view context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文