双倍的 POST 请求而不是单一的

发布于 2024-09-30 01:35:25 字数 2297 浏览 6 评论 0原文

我的对象有一个编辑页面。因为我已将数据划分为两个选项卡,所以我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百变从容 2024-10-07 01:35:25

为什么在保存原始表单后要实例化一个新表单?

if form.is_valid():
    form.save()
    # what's the point of the next line?
    form = EventForm(instance=event)

这几乎肯定是重复 __init__ 调用的原因。你为什么这么做?你已经有了一个表格。

另外,您忘记了在 JS 中停止完成点击操作。

$("#event-data-save").livequery("click", function(e) {
    e.preventDefault();
    ...
})

Why are you instantiating a new form after you save the original one?

if form.is_valid():
    form.save()
    # what's the point of the next line?
    form = EventForm(instance=event)

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.

$("#event-data-save").livequery("click", function(e) {
    e.preventDefault();
    ...
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文