我有一个django项目,带有一个仪表板和带有django admin中构建的表单集的表单。我正在尝试从表单向API发出POST请求。我有两个特定的表单。当我在两个表单集中有多个表单时,出现以下错误。但是,正在通过一种形式。下面是我的代码和错误,
org.w3c.dom.Element element = (Element)svgDoc.getElementById("SVGimage" + value);
if (element == null) continue;
// Removes 'mouseevent' listeners
((EventTarget)element).removeEventListener("mousedown", onDownAction, false);
((EventTarget)element).removeEventListener("mousemove", onMoveAction, false);
((EventTarget)element).removeEventListener("mouseup", onUpAction, false);
((EventTarget)element).removeEventListener("mouseout", onLeaveAction, false);
forms.py
Internal Server Error: /admin/campaign/add
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/views/decorators/cache.py", line 45, in _wrapped_view_func
add_never_cache_headers(response)
File "/usr/local/lib/python3.7/site-packages/django/utils/cache.py", line 252, in add_never_cache_headers
patch_response_headers(response, cache_timeout=-1)
File "/usr/local/lib/python3.7/site-packages/django/utils/cache.py", line 243, in patch_response_headers
if not response.has_header('Expires'):
AttributeError: 'NoneType' object has no attribute 'has_header'
views.py
class CampaignForm(forms.Form):
consumer = forms.CharField(label="Consumer", max_length=200)
startDate = forms.DateTimeField(label="Start Date",
input_formats=['%d/%m/%Y %H:%M'])
endDate = forms.DateTimeField(label="End Date",
input_formats=['%d/%m/%Y %H:%M'])
referreeCredits = forms.IntegerField(label="Referree Credits")
referrerCredits = forms.IntegerField(label="Referrer Credits")
maxReferreeCredits = forms.IntegerField(label="Max Referree Credits")
maxReferrerCredits = forms.IntegerField(label="Max Referrer Credits")
message = forms.CharField(label="Message", max_length=200)
kramerTemplateId = forms.CharField(label="Kramer Template ID", max_length=200)
paymentMode = forms.ChoiceField(label="Payment Mode", choices=[("PAYTM","PAYTM")])
class RuleForm(forms.Form):
eventName = forms.CharField(label="Event Name", max_length=200)
operator = forms.ChoiceField(label="Operator", choices=[("EQUAL","EQUAL"), ("EVERY","EVERY")])
value = forms.IntegerField(label="Value")
class MilestoneRulesForm(forms.Form):
operator = forms.ChoiceField(label="Operator", choices=[("EQUAL","EQUAL"), ("EVERY","EVERY")])
value = forms.IntegerField(label="Value")
referrerCredits = forms.IntegerField(label="Referrer Credits")
HTML模板:
def campaign_add(self, request):
form = CampaignForm()
RuleFormSet = formset_factory(RuleForm)
MilestoneFormSet = formset_factory(MilestoneRulesForm)
if request.method == 'POST':
# import ipdb; ipdb.set_trace()
form = CampaignForm(request.POST)
rule_formset = RuleFormSet(request.POST, prefix='rules')
milestone_formset = MilestoneFormSet(request.POST, prefix='milestones')
if form.is_valid() and rule_formset.is_valid() and milestone_formset.is_valid():
import ipdb; ipdb.set_trace()
dat = {}
dat["consumer"] = form.cleaned_data["consumer"]
dat["startDate"] = self.datetime_to_epoch(form.cleaned_data["startDate"])
dat["endDate"] = self.datetime_to_epoch(form.cleaned_data["endDate"])
dat["referreeCredits"] = form.cleaned_data["referreeCredits"]
dat["referrerCredits"] = form.cleaned_data["referrerCredits"]
dat["maxReferreeCredits"] = form.cleaned_data["maxReferreeCredits"]
dat["maxReferrerCredits"] = form.cleaned_data["maxReferrerCredits"]
dat["message"] = form.cleaned_data["message"]
dat["kramerTemplateId"] = form.cleaned_data["kramerTemplateId"]
dat["paymentMode"] = form.cleaned_data["paymentMode"]
arrRules = rule_formset.cleaned_data
arrMilestoneRules = milestone_formset.cleaned_data
dat["eventRules"] = arrRules
dat["milestoneRules"] = arrMilestoneRules
print(rule_formset.cleaned_data)
print(milestone_formset.cleaned_data)
res = requests.post("https://example.com", data=json.dumps(dat), headers={'content-type': 'application/json'})
if res.status_code == 201 or res.status_code == 200:
messages.success(request, 'Success!')
return redirect("admin:campaign-pg1")
else:
messages.error(request, 'Submission Failed.')
else:
rule_formset = RuleFormSet(prefix="rules")
milestone_formset = MilestoneFormSet(prefix="milestones")
return TemplateResponse(request, "admin/campaign_add.html", {"form":form, "rule_formset": rule_formset, "milestone_formset": milestone_formset})
如何解决此错误,以便我可以在表单集中使用任意数量的表单发出POST请求?
编辑: 这就是我的表单集。当我有多个表格时,出现上述错误。 my formsets
答案 0 :(得分:0)
是否正在达到条件,您的请求的状态代码无效(不是200或201)?在这种情况下,您不会返回任何响应,您只是在打电话:
else:
messages.error(request, 'Submission Failed.')
您的异常的堆栈跟踪表明没有响应对象(您没有从视图返回任何响应)
File "/usr/local/lib/python3.7/site-packages/django/utils/cache.py", line 243, in patch_response_headers
if not response.has_header('Expires'):
AttributeError: 'NoneType' object has no attribute 'has_header'
检查api调用失败的原因并解决问题,然后您应该能够提交表单。
答案 1 :(得分:0)
默认情况下,formset_factory()定义了一种额外的形式。显示的空表格数量由extra参数控制。
MilestoneFormSet = formset_factory(MilestoneForm, extra=2)
RuleFormSet = formset_factory(RuleForm, extra=2)