Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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_Django Csrf - Fatal编程技术网

Python csrf代币跟进

Python csrf代币跟进,python,django,django-csrf,Python,Django,Django Csrf,你好,先谢谢你。这是来自以下线程的后续问题(不确定我是否应该在那里发布或启动新线程…: 我不确定需要对代码执行什么操作才能使csrfContext正常工作。我正在尝试使用ModelForm将数据收集到模型并将其写入MYSQL表。我发现错误: Reason given for failure: CSRF token missing or incorrect. 失败原因: CSRF令牌丢失或不正确。 代码如下: from django.shortcuts import render_to_

你好,先谢谢你。这是来自以下线程的后续问题(不确定我是否应该在那里发布或启动新线程…:

我不确定需要对代码执行什么操作才能使csrfContext正常工作。我正在尝试使用ModelForm将数据收集到模型并将其写入MYSQL表。我发现错误:

Reason given for failure: CSRF token missing or incorrect. 失败原因: CSRF令牌丢失或不正确。 代码如下:

from django.shortcuts import render_to_response from djengo.template import RequestContext from django.http import HttpResponse, HttpRequest, HttpResponseRedirect from acmetest.models import Player from acmetest.models import PickForm csrfContext = RequestContext(request) return render_to_response('makepick.html', csrfContext) def playerAdd(request, id=None): form = PickForm(request.POST or None, instance=id and Player.objects.get(id=id)) # Save new/edited pick if request.method == 'POST' and form.is_valid(): form.save() return HttpResponseRedirect('/draft/') return render_to_response('makepick.html', {'form':form}) 从django.shortcuts导入渲染到响应 从djengo.template导入请求上下文 从django.http导入HttpResponse、HttpRequest、HttpResponseRedirect 从acmetest.models导入播放器 从acmetest.models导入PickForm csrfContext=RequestContext(请求) 返回render\u to\u响应('makepick.html',csrfContext) def playerAdd(请求,id=None): 表单=选取表单(request.POST或None, instance=id和Player.objects.get(id=id)) #保存新的/编辑的拾取 如果request.method==“POST”和form.is\u有效(): form.save() 返回HttpResponseRedirect(“/draft/”) 返回render_to_响应('makepick.html',{'form':form}) 再说一遍

谢谢你的帮助


dpbklyn

我假设我们讨论的是
playerad
视图-您需要将
RequestContext
传递给那里的响应

def playerAdd(request, id=None):
    form = PickForm(request.POST or None,
                       instance=id and Player.objects.get(id=id))
    # Save new/edited pick
    if request.method == 'POST' and form.is_valid():
        form.save()
        return HttpResponseRedirect('/draft/')

    return render_to_response('makepick.html', RequestContext(request, {'form':form}))

代码中的第一行很难理解,甚至看起来都不是有效的python。您不能从函数块外部使用
返回

更新代码:

from django.shortcuts import render
# from djengo.template import RequestContext <- this is not valid.
修改您的回程线:

 # return render_to_response('makepick.html', {'form':form})
   return render(request,'makepick.html',{'form':form})

dict
不应该在
RequestContext
之外,而
render_to_响应不应该是
render_to_响应('makepick.html',{'form':form},RequestContext(request))
@SandipAgarwal它是一个
上下文
子类,所以它可以工作。你更喜欢哪一个?我认为通过RC作为上下文命令更直观。哪一个更有效?谢谢!我不确定我是否清楚…我认为csrf是出于安全目的所必需的。另外,在HTML模板的块中,我假设我不清楚不需要I correct?如果您对提交给视图的表单使用
POST
,则始终需要
{%csrf\u token%}
(除非您明确将视图标记为)。快捷方式为您提供了
RequestContext
要求。刚才看到了这个附加问题。不确定答案有何帮助,但有一件事我必须提到——假设您剪切/粘贴了正确的代码,您将
django.template
拼错为
django.template
。它不是无效代码,只是输入错误。(如果没有,您应该使用RequestContext)。
 # return render_to_response('makepick.html', {'form':form})
   return render(request,'makepick.html',{'form':form})