如何在django JsonResponse中使用jquery DataTables?

时间:2018-02-28 06:05:55

标签: jquery ajax django datatable

我很难实现dataTables的分页。有人能为这个问题提供一个有效的例子吗?我是新的实现jquery / javascript并通过阅读示例来学习它。下面的js脚本正在为同一页面上的其他任务工作,但我仍然坚持在这里实现这个分页。

我的观点是

def ajax_load_questions(request):
    all_questions = Questions.objects.all()
    classroom = request.GET.get('classroom')
    topics = request.GET.getlist('topics_all[]')
    difficulty = request.GET.getlist('difficulty[]')
    subtopics = request.GET.getlist('subtopics_all[]')
    filter1 = all_questions.filter(classroom=classroom).filter(topic__id__in=topics)
    questions_list = filter1.filter(subtopic__id__in=subtopics).filter(difficulty__in=difficulty)

    response_questions = {}
    try:
        response_questions['questions'] = list(questions_list)
    except:
        response_questions['error'] = "No questions available"

    return JsonResponse(response_questions)

我的html文件

<form method="post" id="questionForm" filter-question-url="{% url 'qapp:ajax_load_questions' %}" novalidate>
    {% csrf_token %}
    <table>
        {{form.as_table}}
    </table>
    <br>
    <button id="id_filter_button" type="button">Filter</button>
    <hr>
    <p>Questions</p>

    <div id="id_questions_list">
    </div>
    <hr>
    <button type="submit">Submit</button>
</form>

表格我希望显示

的分页
<table border="1" id="id_question_table">
  <thead>
    <tr>
      <th>Questions</th>
      [other fields]
    </tr>
  </thead>
  <tbody>
    {% for que in questions_list %}
      <tr>
        <td>{{que.question}}</td>
        [other fields]
      </tr>
    {% endfor %}
  </tbody>
</table>

js用于在同一页面上获取其他数据的脚本

$.ajax({
  url: url,
  data: {'classroom':classroomId, 'difficulty':difficulty_list,
        'topics_all':myTopics, 'subtopics_all':mySubtopics},
  success: function (data) {
    $('#id_questions_list').html(data) // I guess this has to be changed but don't know how 
    }
});

我真的被困在这两天了,如果有人可以提供帮助,我会很感激。

0 个答案:

没有答案