如何仅显示过滤器表单?

问题描述 投票:0回答:1

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

enter image description here

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

views.朋友:

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

URL.朋友

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 %}

感谢任何帮助!!

django django-views django-filter
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.