I POST
some data via an HTML form using AJAX to modify data in my database. The data is successfully modified by executing stored procedures on the DB. Final line of the view.py
function call is return redirect('Home')
.
The redirect is successfully executing, but I am not being redirected. I can overcome this by adding window.location.href = 'http://127.0.0.1:8000/
in the success function of the AJAX call. The problem is, I want to use messages.success(request, 'Data Successfully Updated')
, which only appears after refreshing the AJAX-originating redirect.
Is there a reason why return redirect('Home')
is not working here, and is there a way to overcome this?
Home
is the name of my path in urls.py
[06/May/2021 17:23:52] "POST /search/ HTTP/1.1" 302 0 // submitting changes to database
[06/May/2021 17:23:55] "GET / HTTP/1.1" 200 4750 // The page I am intending to redirect to
Ajax Call
function updateCards(){
ajaxUpdCard().done()
function ajaxUpdCard() {
return $.ajax({
type: 'POST',
url: '',
data: {csrfmiddlewaretoken: window.CSRF_TOKEN, Action: 'Card', CardID: $('#currentCardID').val(),
BCard: $('#sr-bad_card').val(), CNum: $('#sr-card_number').val(), CPin: $('#sr-pin_number').val(),
Bal: $('#sr-balance').val()}
})
}
}
views.py
if request.method == 'POST' and request.POST['Action'] == 'Card':
cardID = request.POST['CardID']
Bcard = int(request.POST['BCard'])
CNum = int(request.POST['CNum'])
CPin = int(request.POST['CPin']) if request.POST['CPin'] != '' and request.POST['CPin'] != 'XXXX' else 'Null'
print(CPin)
Bal = request.POST['Bal']
update_card_transaction(entityID, userID, cardID, Bcard, CNum, CPin, Bal)
messages.success(request, 'Data Successfully Updated')
return redirect('Home')