创建搜索字段以对每个列数据进行排序

时间:2018-07-19 17:06:02

标签: python django

我是Django的新手,我的索引页面显示了所有项目,包括project_number列,project_description列等。如果我要搜索项目描述,我希望在每一列中都有搜索字段,它将在同一页面中显示项目描述结果以及它们的project_number,反之亦然。我正在使用ajax,但给了我一个错误

内部服务器错误:/ polls / ajax / validate_username / 追溯(最近一次通话):   在内部的文件“ /home/pdinh/environments/newproject/newenv/lib/python2.7/site-packages/django/core/handlers/exception.py”,第41行     响应= get_response(请求)   文件“ /home/pdinh/environments/newproject/newenv/lib/python2.7/site-packages/django/core/handlers/base.py”,行198,在_get_response中     “改为不返回。” %(回调。模块,view_name) ValueError:视图polls.views.validate_username没有返回HttpResponse对象。它返回None。 [25 / Jul / 2018 16:06:51]“ GET / polls / ajax / validate_username /?the_post = HTTP / 1.1” 500 16574

有人可以帮我吗?除了ajax,其他所有东西都可以正常工作 以下是我的代码

================ index.html ===================

{% block body %}
<div class = "container">
<!-- <form name = "search_form"  action = "{% url 'search' %}">
        <input type = "text" name = "search" placeholder = "Search"></input>
        <button type = "submit" style = "height: 25px; width: 90px">Search</button><br/>
</form> -->

<!-- <form name="search_form" action="{ url 'search' %}" -->
<form >
<div class = "pagination">
        <table class = "table table-hover table-dark">
                <tr>
                        <th scope = "col">No</th>
                        <th scope = "col">Section</th>
                        <th scope = "col">Project Description</th>
                        <th scope = "col">Project ID</th>
                        <th scope = "col">Employee Name</th>
                        <th scope = "col">Funding Year </th>
                </tr>
                <tr>
                        <!-- <form name = "search_form" action = "{ url 'search' %}"> -->
                        <form name = "search_form" method="GET">
                                {% csrf_token %}
                                <div id="the_post">
                                        <th scope = "col"><input type = "text" name = "search_no" placeholder = "Search"></input></th>
                                        <th scope = "col"><input type = "text" name = "search_section" id="id_section" placeholder = "Search"></input></th>
                                        <th scope = "col"><input type = "text" name = "search" id = "search1" placeholder = "Search"></input><input type="submit" value="Search"></input></th>
                                        <th scope = "col"><input type = "text" name = "search_id" placeholder = "Search"></input></th>
                                        <th scope = "col"><input type = "text" name = "search_name" placeholder = "Search"></input></th>
                                        <th scope = "col"><input type = "text" name = "search_year" placeholder = "Search"></input></th>
                                </div>
                        </form>
                </tr>
                {% if projects %}
                        {% for project in projects%}
                                <tr>
                                        <td> {{project.pro_no}} </td>
                                        <td scope = "row"><a href = "{% url 'emp_detail' project.id %}">{{project.pro_section}}</a></td>
                                        <td>{{project.pro_description}}</td>
                                        <td>{{project.pro_ID}}</td>
                                        <td>
                                                {% for emp in project.employee.all  %}
                                                <table>
                                                        <tr><td>{{emp.first_name}}</td></tr>
                                                </table>
                                                {% endfor  %}
                                        </td>
                                        <td>
                                                {% for emp in project.employee.all  %}
                                                <table>
                                                        <tr><td>{{emp.funding_year}}</td></tr>
                                                </table>
                                                {% endfor  %}
                                        </td>
                                </tr>
                        {% endfor %}
                {% endif %}
                <span class = "step-links">
                        {% if projects.has_previous %}
                                <i class = "fa fa-angle-double-left" style="color:black"></i>
                                <a href = "?page={{projects.previous_page_number}}">Previous</a>
                        {% endif %}
                        <span class = "current">
                                Page {{projects.number}} of {{projects.paginator.num_pages}}.
                        </span>
                        {% if projects.has_next %}
                                <i class="fa fa-angle-double-right" style="color:black"></i>
                                <a class="fa fa-angle-double-right" style="color:black" href = "?page={{projects.next_page_number}}">Next</a>
                        {% endif %}
                <span>
        </table>

        {% block javascript %}
                <script>
                        $.ajax({
                                url: '{% url "validate_username" %}', 
                                type: "GET",
                                data: {'the_post': $("#search1").val()},
                                success: function(data){
                                        $("#search1").remove('');
                                        alert("data:")
                                },
                        });
                </script>
        {% endblock %}
</div>
</form>
<!-- </form> -->
</div>
                <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
{% endblock %}

======================= VIEWS.PY ============

def validate_username(request):
        if request.method == "GET":
                description = request.GET.get('the_post')
                #response_data = {}
                #response_data['pro_description'] = Project.objects.filter(pro_description__icontains = description)
                data = {
                        'pro_description': Project.objects.filter(pro_description__icontains = description)
                }               

                #return HttpResponse(json.dumps(response_data), content_type = "application/json")
                return JsonResponse(data)

================= URLS.PY =============

url(r'^ajax/validate_username/$', views.validate_username, name = 'validate_username'),

0 个答案:

没有答案