Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data between different views in Django

I have a basic view that retrieves some data, renders my page and sends some data to this page:

def myview(request)

    one = values.objects.get(user=request.user).address
    two = values.objects.get(user=request.user).number

    return render(request, "main/mytemplate.html", 
                  context={'address': one, 'numbers': two})

So the values retrieved by those two queries are shown on my page.

Now, on the same page, called mytemplate.html, i'm using another view, which is supposed to handle a form and some other operations:

def secondview(request):

    if request.method == 'POST':
        if 'button1' in request.POST:
            form = MyForm(request.POST)
            # check whether it's valid:
            if form.is_valid():
                profile = form.save(commit=False)
                profile.user = request.user
                profile.save()
                return HttpResponseRedirect(request.path_info)


    else:
        form = MyForm()

    return HttpResponse('it works!')

How can i use the data retrieved by those two queries in the second view? The queries are executed when the page is loaded by the first view. Then, in the same page the second view is used. I want to use the two variables one and two in the second view. Is there a way to do this in Django?

Why don't you make the same queries in the second view? Because i would like the second form to be as fast as possible in terms of reload, without having to do a DB query each time that view is used. Also, since i already retrieved those values when the page is opened, it would be a waste to do that again.

I don't know if this question is clear enough, but the core of it is: can i pass variables/data between two views in django?

like image 993
Jack022 Avatar asked Nov 15 '19 19:11

Jack022


People also ask

What is a more efficient way to pass variables from template to view in Django?

Under normal usage the standard is to use URL (GET) variables when you are retrieving data from the server and to use Form (POST) variables when you want to manipulate (edit/delete) data on the server.

Can we create multiple views in Django?

Yes, this works not only for views.py but also for models.by or tests.py .

How many views are there in Django?

Django has two types of views; function-based views (FBVs), and class-based views (CBVs).


2 Answers

You have few options:

  1. Simplest way: include this data in request to the second view (as part of the form data, see an example below). You might even use a single view: if POST was send - store data else do request and show it on a page.
  2. Use cache for that (see an example below) - But I'd recommend to use Django built-in package. Here is a basic example how to use it
  3. Use Django Sessions (see an example below) - it is working option despite of that they have another purpose. When customer is loaded Django will load full session record, so you'll have all data in request.session variable. But that is bad practice: you can get a lot of data duplication and increased database memory consumption.
  4. Use API (e.g. using DjangoRestFramework) together with usual Django app. So you'll just get data you need, and when you need. These API requests can also be cached so it is fast solution.
like image 103
wowkin2 Avatar answered Sep 29 '22 08:09

wowkin2


Yes, you can use session to pass data across views. A session works like a temporary server storage and keeps the needed data in a dictionary form.

For instance, add the following lines to myview:

request.session['one'] = one
request.session['two'] = two

Then, retrieve the data in secondview by referring to the session:

one = request.session['one']
two = request.session['two']
like image 38
Arn Avatar answered Sep 29 '22 10:09

Arn