Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 德扬戈。如何发表评论最简单的例子。CSRF验证失败_Python_Django_Comments_Foreign Key Relationship_Django Csrf - Fatal编程技术网

Python 德扬戈。如何发表评论最简单的例子。CSRF验证失败

Python 德扬戈。如何发表评论最简单的例子。CSRF验证失败,python,django,comments,foreign-key-relationship,django-csrf,Python,Django,Comments,Foreign Key Relationship,Django Csrf,我想创建一个非常简单的网站,每个人都可以发布一首诗,每个人都可以评论其他的诗。所以:模板上有一首诗,还有很多评论。我知道,有Django Discuss-framework用于评论,但我是Django新手,所以我想逐步学习,Django Discuss当时对我来说太难了。 我试着这样做,只是为了训练: models.py: from django.db import models class Poem(models.Model): title = models.CharField(ma

我想创建一个非常简单的网站,每个人都可以发布一首诗,每个人都可以评论其他的诗。所以:模板上有一首诗,还有很多评论。我知道,有Django Discuss-framework用于评论,但我是Django新手,所以我想逐步学习,Django Discuss当时对我来说太难了。
我试着这样做,只是为了训练:

models.py:

from django.db import models

class Poem(models.Model):
    title = models.CharField(max_length = 200)
    text = models.TextField()
    like = models.IntegerField(default = 0)


def __str__(self):
    return self.title

class Comment(models.Model):
    title = models.CharField(max_length = 200)
    text = models.TextField()
    nick = models.CharField(max_length= 100)
    poem = models.ForeignKey(Poem, null=True)
views.py:

from django.core.context_processors import csrf
from django.http import HttpResponseRedirect

from poems.models import Poem, Comment

def comment(request, poem_id):
    if poem_id:
        if request.method == 'POST':
            title = request.POST.get('title')
            text_of_comment = request.POST.get('text_of_comment')
            nick = request.POST.get('nick')
            comment = Comment.objects.create(title = title, text = text_of_comment, nick = nick, poem = Poem.objects.get(id=poem_id))
            comment.save()
            comments = Poem.objects.get(id = poem_id)
            all_comments = comments.comment_set.all()
            args = {}
            args.update(csrf(request))
            args['all_comments'] = all_comments
            return HttpResponseRedirect('/poems/get/%s' % poem_id, args)
        else:
            comments = Poem.objects.get(id = poem_id)
            all_comments = comments.comment_set.all()
            args = {}
            args.update(csrf(request))
            args['all_comments'] = all_comments
            return HttpResponseRedirect('/poems/get/%s' % poem_id, args)
URL.py:

url(r'^comment/(?P<poem_id>\d+)/$', 'poems.views.comment')
url(r'^comment/(?P\d+/$,'poems.views.comment'))
poems.html:

{%  extends "base.html"  %}


{%  block sidebar  %}
        <ul>
            <li><a href="{%  url 'poems.views.poems'  %}">Poems</a></li>
        </ul>
{%  endblock  %}

{%  block content  %}


<h2>{{  poem.title  }}</h2>
<p>{{  poem.text  }}</p>
<p>{{  poem.like  }} person likes this poem.</p>

<a href="/poems/like/{{  poem.id  }}" class="btn btn-success btn-large active"><i class="icon-white icon-heart"></i> Like it!</a>





<form method="post" action="/poems/comment/{{  poem.id  }}/">{% csrf_token %}

    <label for="title">Title</label>
    <p><input type="text" name="title" id="title"></p>
    <label for="text">Comment</label>
    <p><textarea id="text" name="text_of_comment" rows="7" cols="50"></textarea></p>
    <label for="nick">Nick</label>
    <p><input id="nick" name="nick" type="text"></p>
    <input type="submit" class="btn btn-success btn-large" value="OK"/>

</form>
{%  for comment in all_comments  %}
    <h5>{{  comment.title  }}</h5>
    <p>{{  comment.text  }}</p>
    <p>{{  comment.nick  }}</p>
{%  endfor  %}

{%  endblock  %}
{%extends“base.html”%}
{%块边栏%}
{%endblock%} {%block content%} {{poem.title} {{poem.text}

{{poem.like}人喜欢这首诗

{%csrf_令牌%} 标题

评论

刻痕

{所有_评论中的评论%} {{comment.title}} {{comment.text}

{{comment.nick}}

{%endfor%} {%endblock%}
首先:当我运行服务器并发布注释时,它返回:

禁止(403)

CSRF验证失败。请求被中止

我不明白为什么,因为我所有的其他函数和模板都是以相同的方式构建的,并且工作正常。 我做错了什么

第二:我在comment()函数中使用ForeignKey()的方法正确吗?这是在一首诗下创作评论的好方法吗?你们怎么解决这个问题


Thanx for anwsers:)

您可以尝试删除您的所有Cookie以解决CSRF问题(我过去也有过同样的问题,删除Cookie后可以正常工作)

如果没有嵌套注释,关于模型的注释是可以的

你没有问,但我会给你一个建议,你的代码,使用反向URL的,因为有一天如果你改变了什么,你将不得不修复任何地方

答案是:

comment()函数重定向到另一个函数:

    `return HttpResponseRedirect('/poems/get/%s' % poem_id, args)`
因此,该url下的函数:
'/poems/get/%s'%poems\u id

应包含CSRF令牌

应该是这样的:

def poem(请求,poem\u id=1):
args={}
poem=poem.objects.get(id=poem\u id)
args['poem']=诗
所有注释=poem.comment\u set.all()
args['all_comments']=所有_comments
参数更新(csrf(请求))
返回render\u to\u response('poems/poem.html',args)


现在它在没有禁止的情况下工作(403):)

您记得导入CSRF吗
来自django.core.context\u处理器导入csrf
只是一个建议使用NoSQL而不是RDBMS来存储注释和诗词它非常适合这种情况。@Deepend是的,我记得。@Sar009我知道RDBMS在某些情况下无效,但该项目只是为了实践。Thanx.+1关于URL反向的用法!我也建议你使用,你的代码也会更好@劳拉,我删除了饼干。它返回相同的错误,但原因不同:失败原因:未设置CSRF cookie。以前:CSRF令牌丢失或不正确。我不知道这是什么意思。。感谢您对反向URL使用的建议,我可能很快就会遇到URL的问题。@trnsnt Thanx,表单是我的下一课;)