csrf_token配置正确,却报错 禁止访问,渲染的页面里也没有csrf的input

小编的错误在于:

class posting(View):
    def get(self, request):
        template = get_template('posting.html')
        moods = models.Mood.objects.all()
        message = '如果要张贴信息,每个字段都要填1...'
        html = template.render(locals())

        return HttpResponse(html)

  

原因:误用HttpResponse,这样子渲染出来的页面,是不会携带csrf_token的,也就是在前端引用  {% csrf_token %}  会失效

 

正确的代码如下:

class posting(View):
    def get(self, request):
        moods = models.Mood.objects.all()
        message = '如果要张贴信息,每个字段都要填1...'
       
        return render(request,'posting.html',locals())

  

 

posted @ 2020-04-15 22:26  本人小白  阅读(441)  评论(0编辑  收藏  举报