双倍的 POST 请求而不是单一的
我的对象有一个编辑页面。因为我已将数据划分为两个选项卡,所以我使用 jquery-ui。在第一个选项卡 (#core_data
) 上,我正在加载对象的主要数据。表单通过 Ajax 提交:
<form method="post" class="object_form" id="event-core-form" action="{% url edit_event_data event.id %}" enctype="multipart/form-data">
{{ form.as_p }}
<p>
<input class="object-submit" id="event-data-save" type="submit" value="Save data">
</p>
</form>
js:
$("#event-data-save").livequery("click", function() {
$("#event-core-form").ajaxSubmit({
"success": function(data) {
data = JSON.parse(data)
$("#core-data").html(data["form"]);
}
});
return false;
})
并运行一个函数,我的调试显示它立即被调用 2 次:
def edit_event_data(request, event_id,):
template_name="management/core_event.html"
event = get_object_or_404(Event, pk=event_id)
form = EventForm(instance=event, data=request.POST)
if form.is_valid():
form.save()
form = EventForm(instance=event)
form_html = render_to_string(template_name, RequestContext(request, {
"event" : event,
"form" : form,
}))
result = simplejson.dumps({
"form": form_html,
"message": "Data saved."
}, cls=LazyEncoder)
return HttpResponse(result, mimetype='application/javascript')
更重要的是,成功提交后再次点击“保存”给了我一个渲染的原始表单:/这是我的表单使用。我认为这可能是我的问题的原因:
class EventForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
logging.debug("form - init")
super(EventForm, self).__init__(*args, **kwargs)
(...)
def save(self, *args, **kwargs):
(...)
logging.debug("form - save")
post = super(EventForm, self).save(*args, **kwargs)
post.save()
因为在我的调试中我可以看到我想太多 __init__'s
2010-10-28 23:35:08,210 DEBUG form - init
2010-10-28 23:35:34,065 DEBUG form - init
2010-10-28 23:35:34,068 DEBUG form - save
2010-10-28 23:35:34,073 DEBUG form - init
2010-10-28 23:35:34,258 DEBUG form - init
2010-10-28 23:35:34,262 DEBUG form - save
2010-10-28 23:35:34,270 DEBUG form - init
任何人都可以帮忙吗?
编辑:
删除 jquery-ui 后仍然相同。删除 livequery 也不会给出任何结果。
I have an edit page for my object. Because I've divided data in two tabs, I'm using jquery-ui. On the first tab (#core_data
) I'm loading object's main data. Form is submited via Ajax :
<form method="post" class="object_form" id="event-core-form" action="{% url edit_event_data event.id %}" enctype="multipart/form-data">
{{ form.as_p }}
<p>
<input class="object-submit" id="event-data-save" type="submit" value="Save data">
</p>
</form>
js:
$("#event-data-save").livequery("click", function() {
$("#event-core-form").ajaxSubmit({
"success": function(data) {
data = JSON.parse(data)
$("#core-data").html(data["form"]);
}
});
return false;
})
and a function is run, and my debug says that it is instantly called 2 times :
def edit_event_data(request, event_id,):
template_name="management/core_event.html"
event = get_object_or_404(Event, pk=event_id)
form = EventForm(instance=event, data=request.POST)
if form.is_valid():
form.save()
form = EventForm(instance=event)
form_html = render_to_string(template_name, RequestContext(request, {
"event" : event,
"form" : form,
}))
result = simplejson.dumps({
"form": form_html,
"message": "Data saved."
}, cls=LazyEncoder)
return HttpResponse(result, mimetype='application/javascript')
What more, after successfully submitting hitting save once again gives me a raw form rendered :/ Here's the form I'm using. I think that this may be the reason for my problems:
class EventForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
logging.debug("form - init")
super(EventForm, self).__init__(*args, **kwargs)
(...)
def save(self, *args, **kwargs):
(...)
logging.debug("form - save")
post = super(EventForm, self).save(*args, **kwargs)
post.save()
Since in my debug I can see I think too many __init__'s
2010-10-28 23:35:08,210 DEBUG form - init
2010-10-28 23:35:34,065 DEBUG form - init
2010-10-28 23:35:34,068 DEBUG form - save
2010-10-28 23:35:34,073 DEBUG form - init
2010-10-28 23:35:34,258 DEBUG form - init
2010-10-28 23:35:34,262 DEBUG form - save
2010-10-28 23:35:34,270 DEBUG form - init
Can anyone help ?
EDIT:
After removing jquery-ui still the same. Removing livequery also gives no results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么在保存原始表单后要实例化一个新表单?
这几乎肯定是重复
__init__
调用的原因。你为什么这么做?你已经有了一个表格。另外,您忘记了在 JS 中停止完成点击操作。
Why are you instantiating a new form after you save the original one?
This is almost certainly the cause of your duplicated
__init__
call. Why are you doing it? You already have a form.Also, you've forgotten to stop the click action from completing within your JS.