无法查看帖子
有人可以告诉我为什么以下视图不会接收 POST 请求:
# Loads all the latest phone numbers for the models.py file
def client_phones_form_view(request, clientKEY):
try:
i_clientKEY = int(clientKEY)
except ValueError:
raise Http404()
phones = []
# Populates a list with the latest phone numbers for ALL types of phone
for k, v in PHONE_CHOICES:
try:
phones.append(ClientPhone.objects.filter(client=i_clientKEY, phone_type=k).latest('id'))
except ClientPhone.DoesNotExist:
pass
if request.method == 'POST':
formPhone = ClientPhoneForm(request.POST)
if formPhone.is_valid() and formPhone.number.clean(formPhone.number):
c = Client.objects.get(id=i_clientKEY)
formPhone.CustomSave(c, request.user)
return render_to_response(...)
else:
return render_to_response(...)
else:
formPhone = ClientPhoneForm()
return render_to_response(...)
我知道当我提交表单时它会重新加载,但它总是会重新加载底部 render_to_response
编辑:
javascript 看起来像这样:
$( "#newPhoneDialog" ).dialog({
autoOpen: false,
height: 200,
width: 350,
modal: true,
buttons: {
"Add New Phone Number": function() {
var bValid = true;
//alert($('#id_number').val()+" - "+$('#id_phone_type').val());
bValid = bValid && checkNumberLength( phoneNumber, "the phone number", 11, 15 );
bValid = bValid && checkNumberRegexp( phoneNumber, /^([0-9])|( \t)+$/, "The phone number may only consist of numbers and spaces");
if ( bValid ) {
$('.error').hide(); // hide the error div
$('.success').hide(); // hide the success div
$('.info').hide(); // hide the information div
$.ajax({ // create an AJAX call...
data: $('#newPhoneForm').serialize(), // get the form data
type: 'POST', // GET or POST
url: 'client/form/phones/1/', // the file to call
success: function(response) { // on success..
$('#clientPhonesForm').html(response); // update the main phones DIV
}
});
$( this ).dialog( "close" );
//return false; // cancel original event to prevent form submitting
}
},
Cancel: function() {
// reloads the phone div because re-selecting the first item in the options doesn't work
$('#clientPhonesForm').load('{% url client.views.client_phones_form_view clientKEY %}');
$( this ).dialog( "close" );
}
},
close: function() {
// reloads the phone div because re-selecting the first item in the options doesn't work
//$('#clientPhonesForm').load('{% url client.views.client_phones_form_view clientKEY %}');
}
});
HTML 看起来像这样:
<div id="newPhoneDialog" title="Create New Phone Number">
Please enter a valid phone number:
<div id="container">
<form action="{% url client.views.client_phones_form_view clientKEY %}" method="POST" id="newPhoneForm">
<table>
{% for field in formPhone %}
<tr><td>{{ field.label_tag }}</td><td>{{ field }}</td></tr>
{% endfor %}
</table>
</form>
</div>
</div>
编辑
如果它说“How do I (jquery ajax) Submit a form when a button is clicked'”也许这个问题会更好
Can someone tell me why the following view won't pick up a POST request:
# Loads all the latest phone numbers for the models.py file
def client_phones_form_view(request, clientKEY):
try:
i_clientKEY = int(clientKEY)
except ValueError:
raise Http404()
phones = []
# Populates a list with the latest phone numbers for ALL types of phone
for k, v in PHONE_CHOICES:
try:
phones.append(ClientPhone.objects.filter(client=i_clientKEY, phone_type=k).latest('id'))
except ClientPhone.DoesNotExist:
pass
if request.method == 'POST':
formPhone = ClientPhoneForm(request.POST)
if formPhone.is_valid() and formPhone.number.clean(formPhone.number):
c = Client.objects.get(id=i_clientKEY)
formPhone.CustomSave(c, request.user)
return render_to_response(...)
else:
return render_to_response(...)
else:
formPhone = ClientPhoneForm()
return render_to_response(...)
I know when I submit the form it's reloading, but it always reloads the bottom render_to_response
EDIT:
The javascript looks something like this:
$( "#newPhoneDialog" ).dialog({
autoOpen: false,
height: 200,
width: 350,
modal: true,
buttons: {
"Add New Phone Number": function() {
var bValid = true;
//alert($('#id_number').val()+" - "+$('#id_phone_type').val());
bValid = bValid && checkNumberLength( phoneNumber, "the phone number", 11, 15 );
bValid = bValid && checkNumberRegexp( phoneNumber, /^([0-9])|( \t)+$/, "The phone number may only consist of numbers and spaces");
if ( bValid ) {
$('.error').hide(); // hide the error div
$('.success').hide(); // hide the success div
$('.info').hide(); // hide the information div
$.ajax({ // create an AJAX call...
data: $('#newPhoneForm').serialize(), // get the form data
type: 'POST', // GET or POST
url: 'client/form/phones/1/', // the file to call
success: function(response) { // on success..
$('#clientPhonesForm').html(response); // update the main phones DIV
}
});
$( this ).dialog( "close" );
//return false; // cancel original event to prevent form submitting
}
},
Cancel: function() {
// reloads the phone div because re-selecting the first item in the options doesn't work
$('#clientPhonesForm').load('{% url client.views.client_phones_form_view clientKEY %}');
$( this ).dialog( "close" );
}
},
close: function() {
// reloads the phone div because re-selecting the first item in the options doesn't work
//$('#clientPhonesForm').load('{% url client.views.client_phones_form_view clientKEY %}');
}
});
And HTML something like this:
<div id="newPhoneDialog" title="Create New Phone Number">
Please enter a valid phone number:
<div id="container">
<form action="{% url client.views.client_phones_form_view clientKEY %}" method="POST" id="newPhoneForm">
<table>
{% for field in formPhone %}
<tr><td>{{ field.label_tag }}</td><td>{{ field }}</td></tr>
{% endfor %}
</table>
</form>
</div>
</div>
EDIT
Maybe this question would be better if it said 'How do I (jquery ajax) submit a form when a button is clicked'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个防弹条件:
if request.method == 'POST'
因此我将开始研究为什么您没有生成POST
请求,而不是视图。您能确保您是通过表单发布的吗?
开发服务器是否正在注册 POST 请求?应该说
"GET /..."
或"POST /..."
更新:好的,所以这是一个 ajax 请求表单。最近 SO 上的另一个人也遇到了类似的问题,因为 AJAX 请求将原始表单触发为 GET 请求。
我看到你注释掉了
return false
——这可能是问题所在吗?实际上没关系,我不认为对话框按钮会干扰。更重要的是:开发服务器记录的是什么?先 POST 然后 GET?单个 GET?
That's a bullet proof condition right there:
if request.method == 'POST'
so I would start looking at why you are not generating aPOST
request, not the view.Can you make sure that you're posting from your form?
<form method="post">
Is the dev server registering a POST request ? Should say
"GET /..."
or"POST /..."
Update: OK so it's an ajax request form. There was another person on SO recently that had a similar issue because the AJAX request was triggering the original form as a GET request.
I see you commented out
return false
-- could that be the problem? Actually nevermind, I don't think a dialogs buttons would interfere.More importantly then: what is the dev server recording? A POST then a GET? A single GET?
以下似乎就是答案。不知道正确与否:
The following seemed to be the answer. Whether it is correct or not I don't know: