Khamey
Khamey

Reputation: 491

HttpResponseRedirect not changing webpage

I have a webpage that creates and saves data to a database. To save the data, the user must hit the save button and I want to redirect from the create/save webpage to the success webpage that just says "success!"

views.py

@csrf_exempt
def save(request):
    if request.method == 'POST':
        rawdata1 = request.body
        rawdata2 = json.loads(rawdata1)
        length = len(rawdata2)
        for i in range(0,length,1):
            x = meterdata(time_elapsed=rawdata2[i]['time_elapsed'], volts=rawdata2[i]['volts'], amps=rawdata2[i]['amps'], kW=rawdata2[i]['kW'], kWh=rawdata2[i]['kWh'], session=rawdata2[i]['session'])
            x.save()        
        return HttpResponseRedirect(reverse('meter:success'))

def success(request):
    return render(request, 'success.html')

In the developer tools I can see the post to the database (I have also checked it is saving data), and I can see success.html returned in the response but the actual web browser does not navigate to the webpage /sessionsimulator/success.

Upvotes: 0

Views: 95

Answers (1)

karthikr
karthikr

Reputation: 99620

If you are using javascript libraries to do the post, HttpResponseRedirect would not work.

Alternatively, you can send back the success message (or the URL) as a response and then in the success handler (io.success) either alert the success message, or to redirect to the desired page.

Upvotes: 1

Related Questions