繁体   English   中英

django ModelForm:html中的表单无法输入数据并提交

[英]django ModelForm: the form in the html cannot enter data and submit

我创建了一个模型表单,但是在页面中,它只能显示表单,但是在单击“提交”按钮后无法输入数据且没有响应。 我的视图或模板有什么问题吗?

打开页面时,没有错误提示。

谢谢你的帮助。

views.py

from django.http import JsonResponse

class ResultView(ListView):
    context_object_name = 'result_list'
    template_name = 'result_list.html'

     # /URL-to-ajax-view/
    def ajax_get_queryset(request):
        if self.request.method == 'POST' and request.is_ajax:

            form = InputForm(self.request.POST or None)
            if form.is_valid():
                company = form.cleaned_data['company']
                region = form.cleaned_data['region']

                queryset=Result.objects.filter(company=company,region=region)
                sales=Result.objects.queryset.aggregate(Sum('sales'))

            return render(request, 'dupont', {'sales': sales,'region':region,'queryset': queryset})
    else:
        form=InputForm()

result_list.html

<style type="text/css">

.basicinfo {
position: absolute;
width: 200px;
height: 115px;
left: 22px;
top: 0px;   
}

<body>
<div class="basicinfo">
<form id="InputForm" method="post" action="">   #here is the data entry form
        {% csrf_token %}

        <!--enter the company name--> 
        <div class="field">
            {{ form.company.errors }}
            <label id="id_company" name="company" for="{{ form.company.id_for_label }}">Company:</label>
            {{ form.company }}
        </div>

        <!--select region-->
        <div class="field" >
            <label> Select the Region:
            {{ form.region }}
                {% for region in form.region.choices %}
                     <option value="region" name= "region" id="id_region">{{region}} </option>
                {% endfor %}
            </label>
        </div>

        <!--submit-->
        <p><input type="submit" value="Submit" /></p></div>
    </form> 
    </div>
</div>

<script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script type="text/javascript">

$(document).ready(function() {
    $("#InputForm").submit(function() { // catch the form's submit event
    var region= $("#id_region").val();
    var company= $("#id_company").val();

        $.ajax({ // create an AJAX call...
            data: $(this).serialize(), // get the form data
            type: $(this).attr('post'),
            url: "dupont_list/",
            success: function(data) { // on success..
                $("#result").html(data); // update the DIV "result"
            }
        });
        return false;
    });
});
</script>


    <div id="result" class="result">   <!--Showing the filtered result in database-->

    <table>
    <tr><b>Sales</b></tr>
    <td> {{sales.sales__sum}}</td>


    <tr><b>Employee</b></tr>
    <td> {{employee.employee__sum}}</td>
    </table>

我在您的代码中发现了一个小错误:

if self.request.method == 'POST' and request.is_ajax:

应该

if self.request.method == 'POST' and request.is_ajax():

注意()

另外:将按钮从提交更改为按钮。

您的表单被html中绝对定位的元素所遮盖。 尝试将其添加到您的CSS:

form{ position:absolute;z-index:100 }

这将使您的窗体超出阴影的范围。

暂无
暂无

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

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