gpt4 book ai didi

python - Django表单错误: 'QueryDict' object has no attribute 'method' when POST assigned

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:28 24 4
gpt4 key购买 nike

我在使用特定表单时遇到问题,该表单不允许我将数据发布到我定义的函数。我很困惑为什么这会给我带来错误,因为我使用几乎相同的表单在同一网站中执行另一个操作。

当我发布此数据时,Django 抛出“'QueryDict'对象没有属性'method'”,即使我已将该方法指定为 POST。当它没有发布时,它会很好地呈现表单,只有当我提交时才会出现问题。\

Django抛出的错误指向 View 中的这一行:if request.method == "POST":这是下面“取消”函数中的第一行。

任何人都可以确定我在这里做错了什么吗?我很茫然。

这是 view.py 中的函数:

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from .apps.instant import orderlist, orderdetails, cancel
from .forms import cancelorder

def cancel(request):
if request.method == "POST":
form = cancel(request.POST or None)
if form.is_valid():
response = cancel(request.session['token'],form.cleaned_data['orderId'], form.cleaned_data['reason'])
return render(request, 'instant/review.html', {'data':response})
else:
form = cancelorder()
return render(request, 'instant/cancel.html', {'form':form})
else:
form = cancelorder()
return render(request, 'instant/cancel.html', {'form':form})

这是 forms.py:

from django import forms

class cancelorder(forms.Form):
orderId = forms.CharField(label='Order Id', max_length=5)
reason = forms.CharField(label='reason', widget=forms.Textarea)

这是它正在呈现的模板(instant/cancel.html):

{% extends "main/header.html" %}
{% load widget_tweaks %}
{% block content %}
<div class = "row">
<div class ="col-sm-3">
</div>
<div class ="col-sm-6">
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Warning!</strong> When you cancel, the host gets an email notifiaction! Also, there are no confirmations. Only click cancel if you're sure!
</div>
</div>
</div>
<div class = "row">
<div class ="col-sm-3">
</div>
<div class ="col-sm-6">
<div class="well-lg" style="background-color:#efefef">
<center>
<h3> cancel an order</h3>
</center><br>
<!-- <h3>Login</h3> -->
<form action="/instant/cancel/" method="POST" class="post-form">{% csrf_token %}
<div class="form-group">
<label for="login_email">Order Id</label>
{{ form.orderId|add_class:"form-control"|attr:"placeholder:Enter numbers only"}}
</div>
<div class="form-group">
<label for="login_password">Reason</label>
{{ form.reason|add_class:"form-control"|attr:"placeholder:Why are you cancelling?"}}
</div>
<button type="submit" class="btn btn-danger btn-lg">Send Cancellation</button>
</form>
</div>
</div>
</div>
{% endblock %}

最后是表单收集数据的函数。 .apps.instant(我知道我调用的 API 可以正常工作):

from time import sleep
import requests
import logging
import json

LOG_FILENAME = 'transaction_log.log' #Production Log
logging.basicConfig(filename=LOG_FILENAME,level=logging.INFO,format='%(asctime)s %(message)s')

url = APIURL (removed for stackoverflow)

def cancel(token,orderId,reason):
auth_token = {"Authorization" : "bearer " + str(token)}
raw = {'instabookingId':orderId,"reason":reason}
info = json.dumps(raw)
cancellation = requests.post(url, headers=auth_token,data=info)
response = cancellation.json()
return response

感谢您提供的任何帮助,我真的很想知道为什么这不起作用。

这是 Django 的错误:

Environment:


Request Method: POST
Request URL: http://164.132.48.154:8000/instant/cancel/

Django Version: 1.10.2
Python Version: 2.7.9
Installed Applications:
['main',
'instant',
'widget_tweaks',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/root/mysite/instant/views.py" in cancel
24. form = cancel(request.POST or None)

File "/root/mysite/instant/views.py" in cancel
23. if request.method == "POST":

Exception Type: AttributeError at /instant/cancel/
Exception Value: 'QueryDict' object has no attribute 'method'

最佳答案

错误在这一行

...
def cancel(request):
if request.method == "POST":
form = cancel(request.POST or None) # you call the function, its the same name, you would call your cancelform
if form.is_valid():
response = cancel(request.session['token'],form.cleaned_data['orderId'], form.cleaned_data['reason'])
return render(request, 'instant/review.html', {'data':response})
else:
...

是递归的,第二次,在函数cancel中,request == request.POST,而request.POST没有属性'method'

关于python - Django表单错误: 'QueryDict' object has no attribute 'method' when POST assigned,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com