使用django

时间:2016-11-06 09:54:47

标签: python ajax django

我正在制作一个django项目并制作一个仪表板,我正在尝试添加一个搜索栏,在键盘中我将获得所有相关搜索的列表。

Views.py

def get_user_name(request):
    if request.is_ajax():
        results = []
        context = RequestContext(request)
        if request.method == 'GET':
            q = request.GET.get('search_text')
            results = User.objects.filter(user_name__contains = q)
            context = {
                'data': []
            }
            for record in results:
                data = {
                    'id': record.id,
                    'code': record.code,
                    'name': record.name,
                    'manager': record.manager.email,
                    'type': record.type.upper(),
                    'status': record.current_state.display_name,
                }
        return render(request, 'listinguser.html', context)

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

我面临的问题: 当我在搜索框中输入内容时,会调用ajax func,但它总是会进入错误块。

此外,我希望搜索结果可以像表格一样填充在表格中。

这是我用来填充表格的API。

class UserListingView(View):

    def get(self, request):
        try:
            page_size = int(request.GET.get('page_size', 10))
            page_no = int(request.GET.get('page_no', 1))
        except ValueError:
            # Set the default value
            page_size = 100
            page_no = 1

        filter_on = request.GET.get('current_state', None)
        if 'listing_user' in request.user.permissions:
            users = Users.objects.select_related(
                "user", "user_state", "user_manager"
            ).all()
        else:
            users = User.objects.select_related(
                "user", "user_state", "user_manager"
            ).filter(user_manager=request.user)

        paginator = Paginator(integrations, page_size)
        context = {
            'page': {},
            'data': []
        }
        try:
            page = paginator.page(page_no)
            context['current_page'] = page_no
        except EmptyPage:
            # Show the last page
            page = paginator.page(paginator.num_pages)
            context['current_page'] = paginator.num_pages

        for record in page:
            data = {
                'user_id': record.id,
                'user_code': record.user_code,
                'user_id': record.user_id,
                'user_name': record.user_name,
                'user_manager': record.user_manager.email,
                'user_type': record.user_type.upper(),
                'user_status': record.user_state.display_name,
                'user_status': user_status,
            }
            context['data'].append(data)
            context['page'] = page
        return render(request, 'listinguser.html', context)

0 个答案:

没有答案