Saturday, January 30, 2021

Re: How would this be done within the Django framework?

On Sat, Jan 30, 2021 at 08:49:09AM -0800, dazzle.razzle.em wrote:
> I am new to Django and to web development, and am trying to get my bearings.
>
> Suppose that I provide a web page that asks the user for an airport, a
> start date, an end date, a number of days. The server is supposed to go to
> the NOAA website, grab temperature data for the range of dates, and do a
> moving average filter over the given number of days; and then display for
> the client a graph of the smoothed data, constructed maybe with SVG. I
> know how to do the NOAA access and create the graph (if I do it just on my
> computer using Python), but I don't know how to fit the client data
> specification and the server's response into the Django framework. My
> point is that none of this involves Django models at all, and what I am
> having trouble with is: where does my code that does stuff fit in.

I'd start by calling your code from a Django view.

1. You can start with some hard-coded NOAA client request data in the view
2. The response from NOAA probably returns json which you can convert
into Python data structures(dicts, lists) using the json lib.
3. Pass that response data into your template as the context. ex:

return render(request, 'weather.html', context=noaa_response_data)

4. In your template, iterate through the noaa response data to display
it as a table for now.
5. Replace the hard coded noaa client request from step 1 with data from
a form that the user submits (see form tutorial as an example). You may
have to do some work here converting the form data into the data format
that your NOAA client code expects.
6. Add a way to display your SVG graph of the data in your template
in addition to the simple table from step 4

> Can this type of client-server interaction be handled by the Django
> framework, and if so, how would this be structured very generally?

I am a big fan of "type until it works". So you'd start with one Django
view and just type code into that view function until you have your
desired behavior. Messy code is ok. Example here:

https://simpleisbetterthancomplex.com/tutorial/2018/02/03/how-to-use-restful-apis-with-django.html

Then if you want, you can refactor the code to make it
more modular and reusable. I like the services.py pattern:

https://stackoverflow.com/a/30312778/226697

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20210130185306.GN9805%40fattuba.com.

No comments:

Post a Comment