1

I'm setting up a web app/database with the django framework. I'm trying to view the results from a query, with user input values from a form, into a datatable on another page using django-datatables-views.

My_project/search/views.py

class index(View):
    def get(self, request):
        return render(request, 'search/index.html')

class OrderListJson(BaseDatatableView):
    template_name = "search/results.html"
    order_columns = ['column1', 'column2']

    def get_initial_queryset(self):
        db = Database('default', readonly=True)
        qs = Some_table.objects_in(db)

    def filter_queryset(self, qs):
        search_id = self.request.POST.get('textfield', None)
        qs = qs.filter(column1=search_id)
        return qs

    def prepare_results(self, qs):
        json_data = []
        for item in qs:
            json_data.append([
                escape(item.column1),  
                escape(item.column2),
        ])
        return json_data

My_project/search/urls.py

urlpatterns = [
    path('', index.as_view(), name='index'),
    path('search/', OrderListJson.as_view(), name='order_list_json'),
]

My_project/search/template/search/results.html

{% extends 'layout.html' %}
{% block head %}
 <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
 <style type="text/css" class="init">

 </style>
 <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
 <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
 <script type="text/javascript" charset="utf8" src="../../../templates/js/datatableview.js"></script>
 <script type="text/javascript" class="init">


$(document).ready(function() {
    var oTable = $('.datatable').dataTable({
        // ...
        "processing": true,
        "serverSide": true,
        "ajax": "{% url 'order_list_json' %}"
    });
    // ...
});


 </script>
{% endblock %}

{% block content %}
{{ datatable }}
{% endblock %}

I only see the json object.

{"draw": 0, "recordsTotal": 168470170, "recordsFiltered": 2380, "data": [["4059", "233"], ["17099", "198"]], "result": "ok"}

Instead of the datatable embedded in my formated results.html page.

How do I make OrderListJson() render to results.htm?

Thanks!

test_tube_b
  • 85
  • 1
  • 7
  • 1
    I don't understand: your OrderListJson() view returns a JsonResponse, so it does what's expected. What you want is another view to display the "results.html" template (just like your `index` view renders the "index.html" template). The resulting HTML page, when interpreted in the browser, will fetch the json after loading using ajax. – dirkgroten May 17 '19 at 16:03
  • Thank you! I am very new to web development and your help as allowed me understand the doc. – test_tube_b May 17 '19 at 23:08

1 Answers1

0

BaseDatatableView based classes will output JSON data by default. If you want to display the template you can try to use the TemplateView class.