我嘗試向外部 API 發送請求以執行 PATCH 方法。我有一個定義如下的視圖: def dome_view(request, id): ......
我需要 id 以便將它傳遞給表單操作并在外部的其他端點上進行必要的更新。我的 url 模式就像那個路徑('dome2/int:id/', views.dome_view, name='dome_view'),
當我將 id 放到我的表單操作中時,如下所示,我收到一個錯誤“Reverse for 'dome_view' with no arguments not found.”
form action="{% url 'dome_view' id %}"
但是當我輸入要更新的確切 ID 時,PATCH 方法就成功了。 例如:form action="{% url 'dome_view' 5 %}" method="post">
我怎樣才能成功發送 PATCH 請求,而無需每次都指定確切的 id 來形成動作?我只想像 < form action="{% url 'dome_view' id %}" method="post"
我錯過了什么?
我的蟒蛇視圖
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})
謝謝
uj5u.com熱心網友回復:
我通過在渲染請求部分添加 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">
問候
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/342888.html
