I have try to send request to external API in order to perform PATCH method. I have a view defined as below: def dome_view(request, id): ......
I need id in order to pass it to form action and make necessary update on other endpoint which is external.My url pattern is like that path('dome2/int:id/', views.dome_view, name='dome_view'),
When I put id to my form action like below, i got an error "Reverse for 'dome_view' with no arguments not found. "
form action="{% url 'dome_view' id %}"
But when i put the exact id that i want to update, then PATCH method succeded. For example: form action="{% url 'dome_view' 5 %}" method="post">
How can i achieve to successfully send PATCH request without specifiying exact id every time to form action? I just want to do it like <form action="{% url 'dome_view' id %}" method="post"
What am i missing?
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})
Thanks
CodePudding user response:
I fixed it by adding id on render request part: Before
else:
form = ContactForm()
return render(request, 'my_forms/dome2.html', {'contactform':form})
After
else:
form = ContactForm()
return render(request, 'my_forms/dome2.html', {'contactform':form, 'id':id})
And on my html template i added id also
BEfore:
<form action="{% url 'contact' %}" method="post">
After:
<form action="{% url 'contact' id %}" method="post">
Regards