错误消息未显示

时间:2017-10-23 12:13:55

标签: python html django

未显示错误消息。我在index.html中写道

<main>
    <div class="detailimg col-xs-8">
        <div class="relative_ele">

            <div class="container" id="photoform">
                {% if form.errors %} 
                <div class="alert alert-danger" role="alert">
                     <p>At least 1 picture should be selected</p>  
                </div>
                {% endif %}

                <form action="/accounts/upload_save/" method="POST" enctype="multipart/form-data">
                    {% csrf_token %}
                    <div class="input-group">
                        <label class="input-group-btn" style="width: 80px;">
                            <span class="file_select btn-lg">
                                File Select1
                                <input type="file" name="image">
                            </span>
                        </label>
                        <input type="text" class="form-control" readonly="">  
                    </div>
                    <div class="input-group">
                        <label class="input-group-btn" style="width: 80px;">
                            <span class="btn-lg file_select">
                            File Select2
                            <input type="file" name="image2">
                            </span>
                        </label>
                        <input type="text" class="form-control" readonly="">  
                    </div>
                    <div class="form-group">
                        <input type="hidden" value="{{ p_id }}" name="p_id" class="form-control">
                    </div>
                    <div class="col-xs-offset-2">
                        <div class="form-group">
                            <input type="submit" value="SEND" class="form-control">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</main>
在views.py中

@login_required
@csrf_exempt
def upload_save(request):
    if request.method == "POST":
        form = UserImageForm(request.POST, request.FILES)
        if form.is_valid():
            data = form.save(commit=False)
            data.user = request.user
            data.image = request.cleaned_data['image']
            data.save()
        else:
            print(form.errors)
    else:
        form = UserImageForm()

    return render(request, 'registration/photo.html', {'form': form})

在我当前的代码中,当我选择没有图像并且放置&#34; SEND&#34;按钮&amp;我选择了1~2张图片并放入&#34; SEND&#34;按钮,photo.html显示。我想显示错误消息

{% if form.errors  %}
   <div class="alert alert-danger" role="alert">
   <p>At least 1 picture should be selected</p>
   </div>
{% endif %}

当我选择没有图像时,但现在我的系统并不理想。为什么我不能这样做?我想如果我没有选择图像,{% if form.errors %}就变成了现实。我误解了吗?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

<main>
    <div class="detailimg col-xs-8">
    <div class="relative_ele">

        <div class="container" id="photoform">
            {% if form.errors %} 
            <div class="alert alert-danger" role="alert">
                 <p>At least 1 picture should be selected</p>  
            </div>
            {% endif %}

        <!--here change--><form action="/accounts/upload_save/" method="POST" enctype="multipart/form-data">
                {% csrf_token %}
                <div class="input-group">
                    <label class="input-group-btn" style="width: 80px;">
                        <span class="file_select btn-lg">
                            File Select1
                            <!--here change--><input id="file1" type="file" name="image">
                        </span>
                    </label>
                    <input type="text" class="form-control" readonly="">  
                </div>
                <div class="input-group">
                    <label class="input-group-btn" style="width: 80px;">
                        <span class="btn-lg file_select">
                        File Select2
                        <!--here change--><input id="file2" type="file" name="image2">
                        </span>
                    </label>
                    <input type="text" class="form-control" readonly="">  
                </div>
                <div class="form-group">
                    <input type="hidden" value="{{ p_id }}" name="p_id" class="form-control">
                </div>
                <div class="col-xs-offset-2">
                    <div class="form-group">
                        <!--here channge --><input id="send" type="submit" value="SEND" class="form-control">
                    </div>
                </div>
            </form>
        </div>
    </div>
    </div>
</main>



<script type="text/javascript" language="JavaScript">
window.onload = function () {
    var a = document.getElementById("send");
    var file1 = document.getElementById("file1");
    var file2 = document.getElementById("file2");
    a.onclick = function(){ 
        if(file1.value=="" && file2.value==""){
            alert ('You didn\'t choose any of the checkboxes!');
            return false;
        } 
        else {  
            return true;
        }
    }
}

</script> 

希望这会起作用