Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django render not working when redirected from another view

I have two views as follows

def home(request):
    levels = levelData.objects.all()
    context ={
            "levels" : levels
    }
    print "abcde"
    return render(request, 'home.html', context)

and the other one

def create(request):
    if request.method == "POST":
        # doing something with data with saveData as variable of model type
        saveData.save()
        return redirect('home')
    return render(request, 'create.html')

Now, inside the create view, I wish to redirect to home view after saving data. Redirect is working fine and it's being redirected to home as the print statement in home view is executed and "abcde" is printed in terminal. But, home.html does not get rendered and it remains on create.html. Also, the url doesn't change.

This is home view in my urls.py

url(r'^$', home, name='home'),

What am I doing wrong?

like image 464
dr_doof Avatar asked Jan 31 '26 12:01

dr_doof


1 Answers

If you are posting through AJAX, you cannot do that.You have to do like

window.location.href = '/redirecturl/'

in AJAX success callback.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!