如何仅显示过滤器表单?

时间:2019-04-21 20:42:39

标签: django django-views django-filter

因此,我为模型设置了一个过滤器。下面的作品就好了。但是,当我按下home.html上的filter时,它将首先获取并在过滤器表单下方显示整个数据库(请参见屏幕截图)。

enter image description here

我不希望程序首先从数据库中提取所有数据。相反,当我从home.html按过滤器时,我只希望显示过滤器表单,然后等待用户输入并在表单下方显示结果。

views.py:

def search(request):
    option = option2019.objects.get_queryset()
    option_filter = optionFilter(request.GET, queryset=option)
    return render(request, 'option.html', {'filter':option_filter})

url.py

urlpatterns = [
path('simple_upload', views.simple_upload, name='simple_upload'),
path('search/', FilterView.as_view(filterset_class=optionFilter, template_name='option.html'), name='search'),
path('OptionUpdate/<int:id>', views.OptionUpdate.as_view(), name='OptionUpdate'),]

option.html:

{% extends 'base.html' %}

{% load widget_tweaks %}


{% block content %}
{% if user.is_authenticated %}
<form method="get">
    <div class="well container">
        <h4 style="margin-top: 0">Filter</h4>

        {{ filter.form.as_p }}

        <button type="submit" class="btn btn-primary"> Search </button>
    </div>
</form>



<div style="margin: 20px;">
    <h3>Demographics:</h3>
    <table class="table table-bordered">
        <thead class="thead-dark">
            <tr>
                <th>Last Name</th>
                <th>First Name</th>
                <th>DOB</th>
                <th>SSN</th>
                <th>Home Phone</th>
                <th>Phone1</th>
                <th>Phone2</th>
            </tr>
        </thead>

        <tbody>
            {% for hha in filter.qs %}
            <tr>
                <td>{{ hha.LastName }}</td>
                <td>{{ hha.FirstName }}</td>
                <td>{{ hha.dob }}</td>
                <td>{{ hha.SSN }}</td>
                <td>{{ hha.HomePhone}}</td>
                <td>{{ hha.Phone1}}</td>
                <td>{{ hha.Phone2}}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>


<div style="margin: 20px;">
    <h3>Records:</h3>
    <table class="table table-bordered">
        <thead class="thead-dark">
            <tr>

                <th>Option</th>
                <th>Medical Option</th>
                <th>WP Default</th>
                <th>TRN</th>
                <th>PKG</th>
                <th>CELL</th>
                <th>DCA</th>
                <th>HRP</th>
                <th>Remarks</th>
                <th>Option Changed</th>
                <th>Updated PP</th>
                <th>Updated By</th>
            </tr>
        </thead>

        <tbody>
            {% for hha in filter.qs %}
            <tr>

                <td style="background-color: #82b74b">{{ hha.option }}</td>
                <td>{{ hha.medical }}</td>
                <td>{{ hha.WPDefault }}</td>
                <td style="background-color:#b7d7e8">${{ hha.trn }}</td>
                <td style="background-color:#b7d7e8">${{ hha.pkg }}</td>
                <td style="background-color:#b7d7e8">${{ hha.cell }}</td>
                <td style="background-color:#b7d7e8">${{ hha.dca }}</td>
                <td style="background-color:#b7d7e8">${{ hha.hrp }}</td>
                <td>{{ hha.Remarks }}</td>
                <td>{{ hha.OptionChanged }}</td>
                <td>{{ hha.updated }}</td>
                <td>{{ hha.updatedBy }}</td>
                <td>
                    <a href="{% url 'HHA_2019_Records:OptionUpdate' hha.id %}"><span>Edit</span></a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>

{% else %}
{% endif %}
{% endblock %}

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

在您看来,您可以检查表单是否包含数据,例如 bool(self.form.cleaned_data)然后更新模板以根据需要显示或隐藏结果。