简体   繁体   English

如何用来自ajax调用的数据填充POST请求中的Django表单?

[英]How to populate a django form in a POST request with data from an ajax call?

I'm sending form data via an ajax call after user hits submit button and then handling this request in my views, but the form is empty. 用户单击“提交”按钮后,我通过ajax调用发送表单数据,然后在我的视图中处理此请求,但是表单为空。 How can I populate the form? 如何填写表格?

This is my code: 这是我的代码:

My JS file: 我的JS文件:

var csrftoken = getCookie('csrftoken');  //getCookie is a csrf_token generator
var form = $(this).closest('form')
$.ajax({
        url: form.attr("action"),
        data: {        // if I change this line to data: form.serialize(), it works fine!
            csrfmiddlewaretoken : csrftoken,
            form: form.serialize(),  
    },
        type: form.attr("method"),
        dataType: 'json',
        success: function (data)
        {
            //Do a bunch of stuff here
        }
})

My views: 我的看法:

def task_edit(request, pk):
    task = get_object_or_404(Task, pk=pk)

    if request.method == 'POST':
        task_form = NewTaskForm(request.POST) # This is empty!

    else:
        task_form = NewTaskForm(instance=task) # This for populating the edit modal, works fine!

I'm not doing the data: form.serialize() because I need to send additional data with the ajax request. 我没有执行data: form.serialize()因为我需要使用ajax请求发送其他数据。 How do I get this working? 我该如何工作?

You can write as below:- 您可以编写如下:-

$form_data = $("#idofform").serialize();

And then in ajax 然后在ajax中

data : $form_data+'&key=value',

You can use services like https://www.formkeep.com to capture form data using AJAX. 您可以使用https://www.formkeep.com之类的服务来使用AJAX捕获表单数据。

This may be a good idea if you are already using a JavaScript framework, you want to add validation logic, or you don't want to redirect the user after they submit the form. 如果您已经在使用JavaScript框架,想要添加验证逻辑,或者不想在用户提交表单后重定向用户,那么这可能是个好主意。

You can submit to FormKeep using a standard AJAX request. 您可以使用标准AJAX请求提交给FormKeep。 To ensure the request does not cause a redirect, be sure to set the Accept header to application/javascript. 为确保请求不会导致重定向,请确保将Accept标头设置为application / javascript。

Given the following form: 给出以下形式:

<form id="newsletter-signup" action="http://formkeep.com/f/exampletoken" method="POST" accept-charset="UTF-8">
    <input type="hidden" name="utf8" value="✓">
    <input name="email" type="email">
    <input value="Submit" type="submit">
</form>

Here's an example of how to submit a form with jQuery: 这是一个如何使用jQuery提交表单的示例:

$(function() {
   $('#newsletter-signup').submit(function(event) {
event.preventDefault();

var formEl = $(this);
var submitButton = $('input[type=submit]', formEl);

$.ajax({
  type: 'POST',
  url: formEl.prop('action'),
  accept: {
    javascript: 'application/javascript'
  },
  data: formEl.serialize(),
  beforeSend: function() {
    submitButton.prop('disabled', 'disabled');
  }
}).done(function(data) {
  submitButton.prop('disabled', false);
});
});
});

That's it. 而已。 Once your data is securely captured in FormKeep you can easily store it, manage it or connect it with 1000s of other systems like Google Docs and Hubspot and Mailchimp. 一旦在FormKeep中安全地捕获了数据,您就可以轻松地存储,管理数据或将其与数以千计的其他系统(例如Google Docs,Hubspot和Mailchimp)连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM