Search code examples
djangodjango-viewsdjango-urls

How can I go to homepage from another page in Django? I am unable to exit from last called definition


def save(request,id):
    if request.method=='POST':
        if request.POST.get('name'):
            table=Userdb.objects.get(id=id)
            table.name=request.POST.get('name')
            table.url=request.POST.get('url')
            table.phone=request.POST.get('phone')
            table.dob=request.POST.get('dob')
            table.save()
            #messages.success(request,"record saved successfully")
        #return redirect('userlist')
    #return HttpResponseRedirect({%urls 'index'% })
    return render(request,"index.html")

Here at last part,I have directed to index.html. But actually I am not exited the save definition. please refer next snapshot. The url in webpage dont show index.html but shows save. Also css of index webpage is lost.

I tried various urls but didnt work. Also tried to change setings.py still no avail. I want to navigate to home page without trace of any older definition


Solution

  • You should use redirect to go to home page. For URL I suggest to use reverse_lazy

    import following:

    from django.shortcuts import redirect
    from django.urls import reverse_lazy
    

    redirection code:

    return redirect(reverse_lazy("home"))
    

    Please note, for reverse_lazy to work, the name of url should be "home" in urls.py