Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在django中使用ajax从数据库进行搜索_Python_Ajax_Django - Fatal编程技术网

Python 在django中使用ajax从数据库进行搜索

Python 在django中使用ajax从数据库进行搜索,python,ajax,django,Python,Ajax,Django,我正在做一个django项目,并制作了一个仪表板,我试图在其中添加一个搜索栏,在其中键入时,我将获得所有相关搜索的列表 视图.py def get_user_name(请求): if request.is_ajax(): 结果=[] context=RequestContext(请求) 如果request.method==“GET”: q=request.GET.GET('search\u text') 结果=User.objects.filter(User\u name\u contains=

我正在做一个django项目,并制作了一个仪表板,我试图在其中添加一个搜索栏,在其中键入时,我将获得所有相关搜索的列表

视图.py

def get_user_name(请求):
if request.is_ajax():
结果=[]
context=RequestContext(请求)
如果request.method==“GET”:
q=request.GET.GET('search\u text')
结果=User.objects.filter(User\u name\u contains=q)
上下文={
‘数据’:[]
}
记录结果:
数据={
“id”:record.id,
“代码”:record.code,
“name”:record.name,
“经理”:record.manager.email,
“类型”:record.type.upper(),
“状态”:记录。当前状态。显示名称,
}
返回呈现(请求'listinguser.html',上下文)
listinguser.html

{% extends "base.html" %}

{% block title %}Users Listing Page{% endblock %}

{% block body %}
<div class='page_header'>
    <div class="ui-widget">
      <p align="right">
      <input type="text" placeholder="Type user name" id="user_name" name="term">
    </p>
  </div>
  <script>
  $("#user_name").keypress(function(){
        $.ajax({
                 type: "GET",
                 url: "{% url "user_name" %}",
                 data: {
            'search_text' : $('#chain_name').val(),
            'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val()
        },
                 dataType: "json",
                 success: function(response) {
                        //success code
                  },
                  error: function(rs, e) 
                         alert(rs.responseText);
                  }
            });
      })
  </script>
  </div>
    <div class="col-md-6" style="width:100%">
      <table id="data-table" class="table table-striped table-bordered">
        <thead>
          <tr>
            <th style="width: 5%;">#</th>
            <th style="width: 8%;">Id</th>
            <th style="width: 15%;">Name</th>
            <th style="width: 15%;">Status</th>
          </tr>
        </thead>
        <tbody>
          {% for record in data %}
            <tr>
                <td>{{ forloop.counter}}</td>
                <td>{{ record.user_id }}</td>
                <td><a href="/user/{{record.user_id}}/">{{ record.user_name }}</a> <br>({{ record.user_type }})</td>
                <td>{{ record.user_status }}</td>
          {% endfor %}

        </tbody>
      </table>
      {% if page.has_other_pages %}
        <ul class="pagination">
          {% if page.has_previous %}
            <li><a href="?page_no={{ page.previous_page_number }}">&laquo;</a></li>
          {% else %}
            <li class="disabled"><span>&laquo;</span></li>
          {% endif %}
          {% for i in page.paginator.page_range %}
            {% if page.number == i %}
              <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
            {% else %}
              <li><a href="?page_no={{ i }}">{{ i }}</a></li>
            {% endif %}
          {% endfor %}
          {% if page.has_next %}
            <li><a href="?page_no={{ page.next_page_number }}">&raquo;</a></li>
          {% else %}
            <li class="disabled"><span>&raquo;</span></li>
          {% endif %}
        </ul>
      {% endif %}
    </div>
{% endblock %}

你有没有试着观察你的JS代码从浏览器的网络调试工具发出的请求?这里有几个错误,其中最重要的是你没有真正向视图发送
search\u text
;您使用的是与初始完整页面相同的模板来呈现Ajax响应。但这两者都不会导致实际错误;您需要查看浏览器的开发人员工具,以查看Django实际发送的错误。我已编辑了问题..请尝试查看JS代码从浏览器的网络调试工具发出的请求?这里有几个错误,其中最重要的一点是,您实际上并没有向视图发送
search\u text
;您使用的是与初始完整页面相同的模板来呈现Ajax响应。但这两者都不会导致实际错误;您需要查看浏览器的开发人员工具,以查看Django实际发送的错误。我已编辑了问题。请参阅