Here is the flow of my application:
On the index page, users pick a time of the year.
I take them to template with a table of items from the picked period
Users can click on a row to view more details on an item
4 They can then go back to that table or back to the index page
There is a call to the db to fetch the items for the time period. Then when the user selects a row in the table, there is another call to fetch details. What is the most appropriate Django way to accomplish this?
If I was using DRF+JS framework, I would have displayed a pop-up where the fetched details are shown. The user would then be able to close the pop-up and they would be back at the table of items. In regular Django, would you take them to a details view with the option to go back to the table view? And when the user navigates back to the table view, the items for that period are queried again?
You have options. The root question isn’t what “the most appropriate Django way” would be, but where and how you want to spend your time and effort.
First, keep in mind that DRF is a Django package. DRF isn’t doing anything that you can’t do without it - it just makes some things a lot easier or a lot less work.
Django views (with or without DRF) receive requests and return a response. What that response is - a full HTML page, an HTML fragment, JSON, XML, PDF, CSV, etc - is entirely up to you. They’re all “appropriate” in some context.
The choice then comes down to where do you want “control” over the contents of the page being displayed.
- Do you want entire control within the server? (The traditional Django view structure)
- Do you want full control in the browser? (A “pure” SPA built on AJAX requests)
- Or do you want some kind of hybrid model?
Django (with or without DRF) comfortably supports all three.
Thank you for responding! I suppose the root of my question was to see whether the “traditional Django view structure” way of accomplishing this would be what I proposed below. I was probing to see if I was missing a templating technique.
In regular Django, would you take them to a details view with the option to go back to the table view? And when the user navigates back to the table view, the items for that period are queried again?
Is there some reading material you can share that is related to solving this using a view response that is an HTML fragment? I’m thinking if I can send the details while allowing the user to stay on the original table, then that is my best-case scenario.
It’s really no different in principle than any other AJAX-style response.
A view can respond to a request by returning an HttpResponse. One way a response can be created is by using the
render function. That function takes a template and a context and returns the response.
That response doesn’t need to be a full page - it can be just a div. A valid template in this case could be as simple as:
<div>This is a template for an html fragment</div>