Django引导模式文件上载表单

Django引导模式文件上载表单,django,twitter-bootstrap,modal-dialog,Django,Twitter Bootstrap,Modal Dialog,今天我花了大约3个小时在互联网上寻找解决方案,但没有运气。我想有引导模式(弹出窗口),在该模式内我有表格,选择1-6和上传图片文件之间的数字。到目前为止,我有以下文件: Views.py: SUJECTS = ["subject1", "subject3", "subject55",] @login_required def create_exam(request, letnik_id, classes_id, subject_id): response_data = {} i

今天我花了大约3个小时在互联网上寻找解决方案,但没有运气。我想有引导模式(弹出窗口),在该模式内我有表格,选择1-6和上传图片文件之间的数字。到目前为止,我有以下文件: Views.py:

SUJECTS = ["subject1", "subject3", "subject55",] 
@login_required
def create_exam(request, letnik_id, classes_id, subject_id):
    response_data = {}
    if subject_id in SUBJECTS:
        path = letnik_id + '/' + classes_id + '/' + subject_id
        form = ExamForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            exam = form.save(commit=False)
            exam.exam_user = request.user
            exam.exam_path = path
            exam.exam_file = request.FILES['exam_file']
            file_type = exam.exam_file.url.split('.')[-1]
            file_type = file_type.lower()
            if file_type not in IMAGE_FILE_TYPES:
                context = {
                'error_message': 'File must be PNG, JPG ali JPEG',
                }
                return ??
            if Exam.objects.filter(exam_path=path, exam_number=exam.exam_number):
                context = {
                'form': form,
                'error_message': 'Exam already exists!',
                }
                ??return??
            exam.save()
            return redirect('subject_id', letnik_id=letnik_id, classes_id=classes_id, subject_id=subject_id)
        context = {
        "form": form
        }
        return render(request, 'exam_form.html', context)
    raise Http404("Site does not exist")`
我有exam_form.html:

<!-- Modal -->
<div class="modal fade" id="myModalHorizontal" tabindex="-1" role="dialog" 
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <button type="button" class="close" 
                   data-dismiss="modal">
                       <span aria-hidden="true">&times;</span>
                       <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    Dodaj Test
                </h4>
            </div>

            <!-- Modal Body -->
            <div class="modal-body">
            {% if error_messages %}
                        <p style="color: #cc0000;" id="error_message">{{ error_message }}</p>
                    {% endif %}

                <form id="form" class="form-horizontal" role="form" method="post" enctype="multipart/form-data" action="{% url 'create_exam' letnik_id=letnik_id classes_id=classes_id subject_id=subject_id %}" id="post-form">
                {% csrf_token %}
                  <div class="form-group">
                    <label  class="col-sm-2 control-label"
                              for="inputEmail3">Number</label>
                    <div class="col-sm-10">
                        <select class="form-control" id="" name="exam_number">
                          <option>1</option>
                          <option>2</option>
                          <option>3</option>
                          <option>4</option>
                          <option>5</option>
                          <option>6</option>
                        </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-xs-2 control-label"
                          for="inputPassword3" >Picture</label>
                    <div class="col-sm-10">
                        <input  name="exam_file" type="file" required>
                    </div>
                  </div>
                  <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                    </div>
                  </div>






            </div>

            <!-- Modal Footer -->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">
                            CLose
                </button>
                <button type="submit" class="btn btn-primary">
                    Submit
                </button>
            </div>
            </form>
        </div>
    </div>
</div>

&时代;
接近
Dodaj试验
{%if错误消息%}

{{{error_message}

{%endif%} {%csrf_令牌%} 数 1. 2. 3. 4. 5. 6. 图画 接近 提交
它包含在tests.html中(我刚刚输入了按钮,因为test.html的其余部分无关紧要:

<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModalHorizontal" href="">
    Add Exam
</button>
{% include 'exam_form.html' %}

加试
{%include'exam_form.html%}
my views.py:

url(r'^(?P<letnik_id>[1-4])/(?P<classes_id>[A-G])/(?P<subject_id>[\w\-]+)/dodaj$', views.create_exam, name="create_exam"),
url(r'^(?P[1-4])/(?P[A-G])/(?P[\w\-]+)/dodaj$”,views.create_-exam,name=“create_-exam”),

所以我尝试添加引导模式。当我单击“提交”时,会创建考试。但是如果我犯了一个错误,例如,如果考试已经存在或者文件不是png/jpg/jpeg格式,它会给我一个错误,因为我不知道如何配置模式来显示错误和不关闭,如果有任何错误。我见过一些人使用AJAX,但我不知道如何使用实现它。如果可以在不需要重新加载页面的情况下从视图中传递错误,我会喜欢它,因为重新加载页面时,模式会关闭。

javascript中的Catch
submit

$('#form').on('submit', function(e) {
    e.preventDefault();
    $.ajax({
        url : "/url/",
        type: "POST",
        data: $(this).serialize(),
        success: function (data) {
            //here data is the response. Based on this data, do your logic;
        },
        error: function (jXHR, textStatus, errorThrown) {
            alert("error");
        }
    });
});

我尝试了这两种方法(通过ajax和通过action),但问题是相同的,只需删除数据dismission=“modal”就可以了

<div class="modal-footer">
                <button type="button" class="btn btn-default">CLose</button>

接近

我没有你的解决方案,但当我在3小时后遇到类似情况时……第二天你肯定会解决它,放慢速度,解决方案就会出现,因为你已经解决了。谢谢你的回答!我可以问你这个脚本是现成的,还是我必须用我的视图或其他东西配置它?我不得不说,我对Javascript几乎一无所知该脚本应该在html中存在。在相同的html中或通过导入js。您能告诉我如何配置成功:函数(数据){},我将非常感激!在
视图
中,执行类似
的伪代码,如果表单有效,返回JsonResponse({“状态”:“成功”})else返回JsonResponse({“status”:“faliure”})
。因此在javascript中,您可以通过`if data==“success”来检查它vice versaUPDATE:最近一个小时,我一直在尝试用javascript配置我的views.py,但运气不好。我读了一些教程,但没有一个对我有用。.CLose Hi,很抱歉回复太晚。我只是删除了数据Disclose=“modal”从引导模式代码的按钮中,从两个按钮中移除..然后检查..如果我们放置data dismission=“modal”,它将关闭模式,提交操作将不会执行,我建议除了上述场景之外,如果有人想在点击按钮时执行提交操作。。。