How to get a variable from an ajax request and use it in another application in Django?

47 views
Skip to first unread message

kayhan

unread,
Nov 29, 2021, 8:36:22 AM11/29/21
to Django users
How to get a variable from an ajax request and use it in another application in Django?

David Nugent

unread,
Nov 29, 2021, 5:23:37 PM11/29/21
to django...@googlegroups.com
A little more context would help, "use it in another application" is a little vague. The backend the API request and can pretty much do anything you need in the corresponding view at that point but should endeavour to return an appropriate response ASAP.

On Tue, Nov 30, 2021 at 12:35 AM kayhan <kayha...@gmail.com> wrote:
How to get a variable from an ajax request and use it in another application in Django?

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a0d16b47-bb60-46e3-bb5a-53ec718cf172n%40googlegroups.com.

kayhan

unread,
Nov 30, 2021, 2:04:59 PM11/30/21
to django...@googlegroups.com
Sorry I did not ask the question well.
Question:
How to first send some data with an Ajax request to Django view and then with a post request, send the form information to the same view and use the data sent in the previous request (Ajax request) in the second request ?

def planing(request):  
         
    if request.is_ajax():
        # Get user location from user location.js file:
        latitude = request.POST.get('latitude', None)
        longitude = request.POST.get('longitude', None)


    elif request.method == "GET":
        return render(request, "tourist/planing.html")
       
         
    elif request.method == "POST":
# Here I want to take the form data and have
#the previous request data (latitude, longitude) here and do a series of processing.


David Nugent

unread,
Nov 30, 2021, 7:26:50 PM11/30/21
to django...@googlegroups.com
That is definitely much more clear.

The usual way of doing this is to handle it like a shopping cart (plenty of examples only a google search away).
Typically you store this information in the user's session in the first view, then retrieve it in the subsequent view(s).

Note that `request.is_ajax` is deprecated and does not even exist anymore in Django 3.x. You can guess that from other info, but you should probably use a 
different view for handling the ajax request in any case, for clarity and maintainability. The response data would usually use a different format (json vs text/html).

Regards, David

kayhan

unread,
Dec 1, 2021, 10:09:11 AM12/1/21
to django...@googlegroups.com
Thank you David🙏🙏

--
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...@googlegroups.com.

kayhan

unread,
Dec 3, 2021, 7:01:15 AM12/3/21
to django...@googlegroups.com
Hi
Sorry, as you said I used the session.
But my problem has not been solved yet.

I first send a request to Django and save its data with a session.
I will send another request to Django later. Here I want to use the data of the previous request.
But my code does not work properly. And the value "None" appears in the output.
Thankful

def planing(request): if request.is_ajax(): # Get user location from user location.js file: latitude = request.POST.get('latitude', None) longitude = request.POST.get('longitude', None) print("latitude, longitude = ", latitude,longitude) # To save data request.session['latitude'] = latitude request.session['longitude'] = longitude # To retrieve data: latitude = request.session.get('latitude') longitude = request.session.get('longitude') print("latitude, longitude = ", latitude,longitude) elif request.method == "GET": return render(request, "tourist/planing.html") elif request.method == "POST": # To retrieve data: latitude = request.session.get('latitude') longitude = request.session.get('longitude') print("latitude, longitude = ", latitude,longitude) Output: latitude, longitude = 34.801595 48.499574 latitude, longitude = 34.801595 48.499574 latitude, longitude = None None

David Nugent

unread,
Dec 4, 2021, 10:00:08 PM12/4/21
to django...@googlegroups.com
There's a little more to handling session data than what you have there (unless code is missing?).

IIRC you need to set `session.modified = True` in order to persist the data.

Where are you serialising the session data? db, redis .. ?

Regards, David

Reply all
Reply to author
Forward
0 new messages
Search
Clear search
Close search
Google apps
Main menu