【发布时间】:2018-03-14 07:29:49
【问题描述】:
我刚刚开始在我的项目上实现发布请求,但我在使用 csrf 令牌时遇到了问题。
即使看起来我使用正确(我使用渲染,启用 cookie,{% csrf_token %} 在 html 代码中,并且我在 django 设置中有中间件)
编辑: 全视图.py:
def mouvementCreation(request):
idMI = 0
especes = TbEspece.objects.order_by('id')
#Get Mouvement informations
#Connection to 'erp-site' DB
cursor = connections['erp-site'].cursor()
try:
#Get Produits list from Espece
query = "{CALL SP_webGET_PRODUIT_FROM_ESPECE(%s,%s,%s,%s,%s)}"
arguments = (2016, 'C', 0, 10, 'A',)
cursor.execute(query, arguments)
produits = dictfetchall(cursor)
#Get Transporters list
cursor.execute("{CALL SP_webGET_TRANSPORT}")
transporters = dictfetchall(cursor)
#Get Livreur list
cursor.execute("{CALL SP_webGET_LIVREUR}")
livreurs = dictfetchall(cursor)
finally:
cursor.close()
cursor = connections['site'].cursor()
try:
#Get Circuit list
cursor.execute("{CALL SP_webGET_CIRCUIT_FOR_MVT}")
circuits = dictfetchall(cursor)
#Get Source list
cursor.execute("{CALL SP_webGET_SOURCE_FOR_MVT}")
mvtsources = dictfetchall(cursor)
#Get Dest list
cursor.execute("{CALL SP_webGET_DEST_FOR_MVT}")
destinations = dictfetchall(cursor)
#Get PontBascule list
cursor.execute("{CALL SP_webGET_PBASCULE}")
pontBascules = dictfetchall(cursor)
finally:
cursor.close()
reg_normes = TbRegauxnormes.objects.all()
ordreexecs = TbOrdreexecution.objects.all()
form = mouvementForm(request.POST or None)
if form.is_valid():
pont = form.cleaned_data['pont']
dateheure = form.cleaned_data['dateheure']
poid = form.cleaned_data['poid']
dsd = form.cleaned_data['dsd']
typepesee = form.cleaned_data['typepesee']
#Connection to 'erp-site' DB
cursor = connections['pontbascule'].cursor()
try:
#Get Produits list from Espece
query = "{CALL SP_ADD_MANUAL_PESEE(%s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s,%s,%s)}"
arguments = (pont, 'TICKETCHEAT', dateheure, poid, dsd,typepesee, '','','','','','','','','','','','','','','','','')
cursor.execute(query, arguments)
finally:
cursor.close()
return render(request, 'mouvementCreation.html', {'form': form, 'especes' : especes, 'produits' : produits, 'transporters' : transporters, 'livreurs' : livreurs, 'circuits' : circuits, 'mvtsources' : mvtsources, 'destinations' : destinations, 'pontBascules' : pontBascules} )
模板:
<form name="Form1" method="post" action="" enctype="text/plain" id="Form1">
{% csrf_token %}
<input type="text" id="Editbox9" style="position:absolute;left:87px;top:46px;width:86px;height:16px;line-height:16px;z-index:173;" name="pont" value="" spellcheck="false">
<input type="text" id="Editbox34" style="position:absolute;left:87px;top:80px;width:86px;height:16px;line-height:16px;z-index:174;" name="dateheure" value="" spellcheck="false">
<input type="text" id="Editbox35" style="position:absolute;left:87px;top:114px;width:87px;height:16px;line-height:16px;z-index:175;" name="poid" value="" spellcheck="false">
<input type="text" id="Editbox36" style="position:absolute;left:88px;top:153px;width:84px;height:16px;line-height:16px;z-index:176;" name="dsd" value="" spellcheck="false">
<input type="text" id="Editbox37" style="position:absolute;left:87px;top:187px;width:86px;height:16px;line-height:16px;z-index:177;" name="typepesee" value="" spellcheck="false">
<input type="submit" id="Button14" name="submit" value="Submit" style="position:absolute;left:361px;top:65px;width:96px;height:25px;z-index:178;">
</form>
中间件设置:
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
我搜索了多个帖子并尝试了很多修复,但到目前为止没有任何效果,我仍然收到“失败原因:CSRF 令牌丢失或不正确。”
【问题讨论】:
-
您是否使用同一个视图
mouvementCreation来显示表单并处理发布的数据?令牌是否出现在呈现的 html 中(在浏览器中查看源代码以检查)?。 -
当我在浏览器中检查源时,令牌确实出现了。是的,理想情况下我想做的是从视图中的表单中获取信息,然后在我的数据库中调用一个存储过程来创建一个新条目
-
啊,我觉得问题出在
enctype="text/plain"。见this question。 -
是的,这确实是问题所在,非常感谢。我也想知道,因为正如您在我的视图中看到的那样,我在收到表单后调用了一个存储过程,但该过程似乎从未触发(我单独测试过的过程工作)
-
抱歉,这是一个单独的问题,所以我无能为力。
标签: python django django-forms django-csrf