Hello
Since july, I could learn Django and I understand how to make a queryset to a MySQL data and how to pass the value to my template (or gabarit). Then I use
<table>
{% for se in sensor_measures %}
<tr><td>Show some value from se.a_value</td></tr>
{% endfor %}
</table>
To build my graph, I savedthe information in a data tag
data-humidity=""
data-pressure=""
Then with javascript/jQuery, I retrieve the value for Chartjs.
I am not sure, it’s the best way to do, but as it work, I keep it as it is for now.
But now, for map (where ate show all station), I would like to make it a bit better. I am using Leaflet library
in my view, I am going to create a queryset to retrieve the location of all stations. I need to create all markers and assign the latitude and longitude of the stations. Those value are need in my script.js file
Can django interact with a js file instead of a template? If yes how?
Or can we ask a js file to look for the render of a view?
Thanks
Does not it’s the time to study Django Rest Framework?
The answer to this kinda depends upon whether you’re retrieving this data as part of the same page as everything else, or if you’re making an AJAX call to retrieve data from the server after the page has been loaded.
If you’re sending the data as part of the page, see the json_script
tag.
If you’re retrieving data from the server after the page has been loaded, you can call a view that returns a JsonResponse
.
Side note - our internal standard is to always retrieve the data as a separate call, even if it’s needed immediately upon page load.
No, DRF is far more than what you need for this. If you want to use it to learn it, great - but for this application I wouldn’t bother with it.
Hello @KenWhitesell
That’s the question. As I am starting with Django, I want ot make something very simple.
Here is what I did since I started a couple of weeks ago.
In my previous web app build with PHP, I have a chart for different sensors. Then I use AJAX to reload the chart. with Django I make it simplier. i have a page with charts for each sensor. So the need of AJAX would be only for the date/time form, to reload the chart when the date range changed.
I will try the proposition json_script tag and JsonResponse, and start with the first,
But you wrote
if you’re making an AJAX call
If you’re retrieving data from the server after the page has been loaded, you can call a view that returns a JsonResponse
.
Does django offer a ajax method before calling a view that returns a JsonResponse
.?
Or should I did it my js script, something similar I did with PHP
return $.ajax({
url: 'incl/get_measures_new.php', // THAT MUST BE CHANGED TO FIT TO THE PATTERNSURL, ISN'T?
type: 'POST',
data: param,
//data:'type[]=1&y-axis-1=1&'+para,
//data:'type[]=1&'+para, // data: { pond: pond, from: from, to: to },
cache: false,
dataType: 'json',
error: function (request, error) {
console.log(request.responseText);
},
success: function (data) {
}
});
I will better work on that options tomorrow.
MANY thanks again
OK, thanks for your advise
Keep in mind one fundamental point. Everything in Django runs on the server only. There is nothing in Django that runs in the browser. Django is responsible for rendering a page and sending it out to the browser. What happens after it has been sent is solely up to the browser.
The browser communicates with the server through URLs. In the typical view, a browser sends a GET
or POST
request to the server for a URL.
The urls.py files assign those URLs to views.
The views run, and return a Response object. Typically, it’s an HttpResponse.
However, a JsonResponse is also a type of Response object. So the view that returns a JsonResponse is a view like any other view. The only difference is that it returns a JSON object and not rendered HTML.
OK, that’s clear.
One of the goeal to rebuild my PHP web app with django, it’s to perfect some skills that the job market is looking for.
However, a JsonResponse is also a type of Response object. So the view that returns a JsonResponse is a view like any other view. The only difference is that it returns a JSON object and not rendered HTML.
OK, i understand
Then for the browser job, I could copy/past my ajax script or whatever, but that does not fit to my objectif as I want to rebuild my PHP web app to perfect (or get) new skills with some frameworks, as django, vuejs, angular…
May ask you an advise following your expertise with django and as you know a bit my django app.
Regarding the front end (browser) what would you recommend me with Django: vues.js, React, Angular?
Actually, I already start to be familirized with Vue.js because a dev team of my institute work with Vue.js and it’s my favorite for my next step
I saw a tutorial with Angualr and Django which can be helpful
I know the React is nice as well
But which is the “best friend” of Django?
Have a nice week-end
It depends upon the type of app you’re trying to build.
In order of perceived popularity among the people I talk to, it’s React, Vue, then Angular. (Except for those of us that have thrown them all away and have (or are in the process of) moved to HTMX.)
Personally? I don’t create SPAs. I can’t recommend any of them (Vue, React, Angular) - not because they’re bad, but because I haven’t bothered to learn any of them well enough to form a valid opinion. For me, they all turned out to be too much work for what I wanted or needed to do, and the learning curve involved in getting the tool-chains working properly was far more effort for me than I wanted to expend.
So I looked at HTMX, got very impressed very quickly, started working with it - and haven’t looked back since. Does it do everything the other three do? No. But it does everything I need or want it to do.
But at the end of the day, they all work well with Django. It doesn’t matter from Django’s perspective which one you use.