Django的jQuery的表格没有AJAX请求 [英] Django Jquery Form no AJAX request

查看:160
本文介绍了Django的jQuery的表格没有AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读类似主题的其他问题后,我还是不明白什么是错的这个code。

After reading other questions on similar subject, I still do not understand what's wrong with this code.

我测试使用jQuery的表格插件,一个code。我添加视图到模板调用,以显示它的第一时间,使用户可以选择文件并上传。但它永远不会发送Ajax请求,因此没有执行针对code部分。虽然这里没有显示,jQuery库和jQueryForm插件确实被调用。

I am testing a code that uses Jquery Form plugin. I added a call in the view to the template, to display it for 1st time so user can select file and upload. But it never sends the AJAX request, hence the code section in view is not executed. Although not shown here, jQuery library and the jQueryForm plugin are indeed being called.

Template:
<form id="uploadForm"  method="post" enctype="multipart/form-data">
                            {% csrf_token %}
        <input id="fileInput" class="input-file" name="upload" type="file">
        {{ form.docfile }}
        <span class="upload-message"></span>
        <input type="submit" value="Upload" />
</form>
<script>
    var message = '';
    var options = {
        type: "POST",
        url: '/upload/file/',
        error: function(response) {
            message = '<span class="error">We\'re sorry, but something went wrong. Retry.</span>';
            $('.upload-message').html(message);
            $('fileInput').val('');
        },
        success: function(response) {
            message = '<span class="' + response.status + '">' + response.result + '</span> ';
            message = ( response.status == 'success' ) ? message + response.fileLink : message;
            $('.upload-message').html(message);
            $('fileInput').val('');
        }
    };
    $('#uploadForm').ajaxSubmit(options);
</script>

查看:

def upload(request):
    response_data = {}

    if request.method == 'POST':
        if request.is_ajax:
            form = UploaderForm(request.POST, request.FILES)

            if form.is_valid():
                upload = Upload(
                upload=request.FILES['upload']
            )
            upload.name = request.FILES['upload'].name
            upload.save()

            response_data['status'] = "success"
            response_data['result'] = "Your file has been uploaded:"
            response_data['fileLink'] = "/%s" % upload.upload

            return HttpResponse(json.dumps(response_data), content_type="application/json")

        response_data['status'] = "error"
        response_data['result'] = "We're sorry, but kk something went wrong. Please be sure that your file respects the upload conditions."

        return HttpResponse(json.dumps(response_data), content_type='application/json')
    else:
        form = UploaderForm()
        return render(request, 'upload.html', {'form': form})

它确实调用模板在正确的第一次,它显示的按钮,它会再次执行脚本,但形式是无效的,所以RESPONSE_DATA是错误。

It does call template correctly during first time, it displays buttons, it executes the script again but the form is not valid, so response_data is with error.

我在想什么?

谢谢,里卡多

推荐答案

您至少有一个问题可以查看 - 这样的:

You have at least one problem with you view - this:

if not request.GET:
    return render(request, 'upload.html') 

将prevent继续执行时, request.GET中是空的这是做一个POST请求时的情况下

这篇关于Django的jQuery的表格没有AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆