Jaco
Jaco

Reputation: 1694

Django: Models and Views and jQuery DataTables, hello world example

I intuitively would like to understand some basic concepts related to Django and jQuery and how to connect them both related to Models and Data with application with DataTables.

In particular, I looking for basic knowledge in how I can work with a DataTable and return back the result into python code for further manipulation before display. E.g. Assume I market a checkbox next to an item in a DataTable. Meanwhile I can find example how to collect marked items in JS code, but no examples how I get it from JS code into Django views.py, for further manipulation with Python (e.g. retrieve Object.pk for the marked item in DataTable).

Those answers I find involves serialization, json etc, and it just feel as a clumsy ad-hoc solution and likely exist a more smoother/ built-in one. I might be wrong, but as I cannot find good examples to start working with (hello world jQuery and Django Models), I need to ask:

Is there a specific chapter in a book, a video, a blog, article, udemy that can bring me from zero to hero when it comes to Django and Datatables?

Many thanks for some guidance.

Upvotes: 0

Views: 979

Answers (3)

Jaco
Jaco

Reputation: 1694

UPDATE:

Now when I understand some more, I appreciate the answers.

One thing I read that made me better to understand it was which view you look at it.

Above I tried to understand in the perspective from Django, meanwhile it is more correct to understand in the perspective from the browser.

JQuery is like VBA for Excel, you write "macros" and direct the browser to perform certain tasks, including communicating with Django.

Django, on the other hand does mainly only provide what is requested from the front/browser.

Consequently, one need a method to retrieve from Django database and display on the template, Ajax is a technique to use.

Upvotes: 0

nigel222
nigel222

Reputation: 8212

DataTables and Django in combination actually work very well, but you have to understand the division of responsibility, and where the work takes place.

Django is the "back end", running on the server. It retrieves data from a database, and formats it into a table. If you are using Datatables you are relieved of a lot of HTML styling responsibility. You just have to make sure that you emit <table> <tr> <th> and <td> tags to appropriately describe the content of that table. And to generate some simple Jquery, to tell your web browser what to do with that table.

DataTables works in the "front end": the web-browser. It attaches to the <table> you sent, and can format, rearrange, search etc. as the user desires. The server isn't involved.

(DataTables can also fetch data from the server using AJAX for "infinite" tables and suchlike, but that's an advanced subject).

Upvotes: 1

matt.LLVW
matt.LLVW

Reputation: 720

1 Datatables is a jQuery plugin, it is based on it, so you need to have both javascript libraries loaded in your html to use it.Both are related to the Frontend.

2 You connect it by adding statics in your html pages. Django is the Backend, it will process http requests, query the database and return whatever data you'll need.

Read two scoops of django, it's a good base.

Also the tutorial by Mozilla

Upvotes: 1

Related Questions