【问题标题】:sending entire form in ajax and want to read entire form data in django在 ajax 中发送整个表单并想在 django 中读取整个表单数据
【发布时间】:2019-04-25 08:43:18
【问题描述】:

我正在用 ajax 发送整个表单并想在 django 中读取整个表单数据

$(document).ready(function(evt){
    var frm = $('#myform');
    $(document).on('submit','#myform',function(e){
        e.preventDefault();
    var thisForm=frm.serialize();
    var action=frm.attr('action');
    var method=frm.attr('method');
  //var formData = new FormData($('#myform')[0]);
 // console.log("val is"+action);
  alert("vvvv"+action);
//console.log("redmi rowef3grfwefwefjkfeo fem omfe owefm wepocm wpcm wocdm"+formData);  

   $.ajax({
        url: action,
        type: method,
        data: {path:thisForm},
        success: function(data){
                        console.log('upload success!')
                      alert(data)
                      },
        cache: false,
        enctype: 'multipart/form-data',
        processData: false
    });
    });

});
我正在尝试获取数据,但页面上没有显示任何内容
 @csrf_exempt def process_file(request):
       if request.method == 'POST' and request.FILES.getlist('filename'):
           print (request.POST)
           cat_id = json.loads(request.body).get('path')
           print("RESULT: " + str(cat_id))

我想在 process_file 函数中读取数据

【问题讨论】:

  • “它在页面上不显示任何内容”是什么意思?您是否从前端 JS 代码中获得了 console.log 和警报?您是否从后端看到终端中的任何输出(那些print 语句将输出到的位置)?
  • 先生,它在页面上什么都不显示意味着在views.py文件process_file函数中我正在尝试打印 print("RESULT:" + str(cat_id)) 它什么也不打印。在 jquery 中它的工作正常。
  • 如果没有任何东西被打印到控制台,这意味着你没有通过 if 语句(这对我来说看起来不对)。尝试在其上方和下方打印以查看。

标签: javascript jquery django django-forms django-views


【解决方案1】:

您没有将任何东西传递给您的模板,而是使用 Python print() 函数。这些打印语句不会在您的页面上打印数据,而是在您的服务器运行的控制台上打印数据,因为print() 函数适用于控制台而不是网页。

print (request.POST)
print("RESULT: " + str(cat_id))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    相关资源
    最近更新 更多