对 'dome_view' 进行反转,没有未找到的参数。尝试了 1 种模式:['dome2/(?P<id>[0-9]+)/$']
Reverse for 'dome_view' with no arguments not found. 1 pattern(s) tried: ['dome2/(?P<id>[0-9]+)/$']
我已尝试向外部发送请求 API 以执行 PATCH 方法。
我有一个定义如下的视图:
def dome_view(请求, id):
......
我需要 id 以便将它传递给表单操作并在 external.My url 的其他端点上进行必要的更新模式就像那个路径('dome2/int:id/',views.dome_view, 姓名='dome_view'),
当我将 id 放入我的表单操作时,如下所示,我收到错误“'dome_view' 的反向,没有找到未找到的参数。”
form action="{% url 'dome_view' id %}"
但是当我输入要更新的确切 ID 时,PATCH 方法成功了。
例如:form action="{% url 'dome_view' 5 %}" method="post">
如何在每次不指定确切ID的情况下成功发送PATCH请求来形成动作?
我只想像 <form action="{% url 'dome_view' id %}" method="post"
我错过了什么?
我的python观点
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">
此致
我已尝试向外部发送请求 API 以执行 PATCH 方法。 我有一个定义如下的视图: def dome_view(请求, id): ......
我需要 id 以便将它传递给表单操作并在 external.My url 的其他端点上进行必要的更新模式就像那个路径('dome2/int:id/',views.dome_view, 姓名='dome_view'),
当我将 id 放入我的表单操作时,如下所示,我收到错误“'dome_view' 的反向,没有找到未找到的参数。”
form action="{% url 'dome_view' id %}"
但是当我输入要更新的确切 ID 时,PATCH 方法成功了。 例如:form action="{% url 'dome_view' 5 %}" method="post">
如何在每次不指定确切ID的情况下成功发送PATCH请求来形成动作? 我只想像 <form action="{% url 'dome_view' id %}" method="post"
我错过了什么?
我的python观点
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">
此致