Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Storing "State" and Objects for Use Across Several Views

I am new to OOP and web development and am getting used to the definition of objects and such to hold data. I also have a bit of experience with creating basic html websites and doing some state management using React and JS. However, currently I am currently creating a Django website to practice both my OOP skills and Django skills, and I have hit a bit of a snag.

When the homepage of my website loads, a fetch is made to an API that fetches JSON from a server with details such as title, author, and published date. I iterate over the JSON and create a series of Post() objects that contain that information. The Post objects (which are contained in a list at this point) are then passed into the render method in a function in my views.py and then iterated over and the title property is displayed on the html page as a link. I want to store the other properties (author and published date) as metadata somehow in the background. The information always comes from the API, and I do not want to store it in a model on the db. I want to use that metadata information in other views and such (I want it at least available when the user is on the homepage and when he clicks on the link created by the object, I would like the entire Post object info transferred to the new page). However, right now, once the page is rendered, the objects are no longer there because the method that created them has already finished running.

What overarching basic concept of either Django, OOP, or web development am I not grasping so that I can have some sort of "state" on my Django site? In, particular I want to be able to "pass around" temporary information from view to view. I am thinking of creating a class (something like ContentManager) to manage all of the state, but I do not know where it would live. Everything seems pretty static in terms of views. I've worked with web frameworks like React which manages state. So maybe that's where I got that idea from.

It seems like such a simple basic concept, but its been tough to find exactly what I would like to do by Googling it. Most things talk about using Models. At any rate enough rambling. Thanks for the help in advance!

like image 675
Kevin Avatar asked Nov 03 '25 14:11

Kevin


1 Answers

You can use session for this. For example:

def load_api(request):
    data = requests.get('/your/api')
    # handle your API response some how
    meta_data = data.get('meta_data')
    request.session['meta_data'] = meta_data
    return HttpResponse('Your View')

Show that meta_data in another view:

def show_meta_data(request):
    data = request.session.get('meta_data')
    return render(request, 'template', data)

Also, you can delete that meta data from session:

 del request.session['meta_data']

Or django.contrib.auth.logout() it will be cleared, because it uses flush.

like image 139
ruddra Avatar answered Nov 06 '25 03:11

ruddra