我尝试发送请求到外部API,以执行PATCH方法。我有一个定义如下的视图:Def dome_view(request, id):…
我需要id,以便将其传递给形成动作,并在外部的其他端点上进行必要的更新。我的url模式是这样的路径('dome2/int:id/',视图。dome_view name = ' dome_view '),
当我像下面这样把id放到我的表单动作时,我得到了一个错误"反向为'dome_view'没有找到任何参数。">
action =形式"{% url dome_view的id %}">
但是当我输入我想要更新的确切id时,PATCH方法成功了。示例:form action="{% url 'dome_view' 5 %}"方法="post"祝辞
我怎样才能实现成功发送PATCH请求而不指定准确的id每次形成动作?我只是想这样做<<strong>form action="{% url 'dome_view' id %}"方法="post">
我错过了什么?
My python view
def dome_view(request, id):
filled_form = ContactForm(request.POST)
if request.method == 'POST':
if filled_form.is_valid():
''' Begin reCAPTCHA validation '''
recaptcha_response = request.POST.get('g-recaptcha-response')
url = '<google_captchurl>'
values = {
'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY,
'response': recaptcha_response
}
data = urllib.parse.urlencode(values).encode()
req = urllib.request.Request(url, data=data)
response = urllib.request.urlopen(req)
result = json.loads(response.read().decode())
''' End reCAPTCHA validation '''
if result['success']:
url = "<here_api_url_for_auth>"
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
payload={}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic xxxxxxxx'
}
response = requests.request("POST", url, headers=headers, data=payload, verify=False)
tok=response.json().get('response')['token']
p_url = "<here_my_api_url_for_patch>"
p_url += str(id)
payload = json.dumps({
"fieldData": {
"yas": filled_form.cleaned_data['yas']
}
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer '+ tok
}
response = requests.request("PATCH", p_url, headers=headers, data=payload, verify=False)
if response.status_code == 200:
return HttpResponseRedirect("/thank-you")
else:
do smth
else:
do smth
else:
form = ContactForm()
return render(request, 'my_forms/dome2.html', {'contactform':form})
感谢我通过在渲染请求部分添加id来修复它:在
else:
form = ContactForm()
return render(request, 'my_forms/dome2.html', {'contactform':form})
后else:
form = ContactForm()
return render(request, 'my_forms/dome2.html', {'contactform':form, 'id':id})
在我的html模板中,我还添加了id
:
<form action="{% url 'contact' %}" method="post">
:后
<form action="{% url 'contact' id %}" method="post">
对