Sarthak Rohilla
Sarthak Rohilla

Reputation: 25

Django render redirect to page with changing url

I am creating a custom login for my django application and problem occurs when i click logout on template I programmed it so it go to index page but the url in the browser remains http://127.0.0.1:8000/logout/ even reaching index page. I want it to become http://127.0.0.1:8000/ .

views.py

def logout(request):
    try:
        del request.session['uid']
        return render(request, 'home.html')
    except:
        pass
    return render(request, 'home.html')

def home_page(request):
    return render(request, 'home.html')

template

 <p>
    Publisher Dashboard, Welcome {{ user.name }}.
    <a href="{% url 'logout' %}" class="btn btn-primary">Logout</a>
</p>

urls.py

from django.conf.urls import url
from django.contrib import admin
from mainapp import views

urlpatterns = [
url(r'^$', views.home_page, name='homepage'),
url(r'^registration/$', views.signup_user, name='signup_user'),
....
url(r'^logout/$', views.logout, name='logout'),
]

Upvotes: 2

Views: 14614

Answers (2)

Yusuf
Yusuf

Reputation: 449

Render will give a new webpage but not a new URL.

Redirect will give a new URL.

So in case of render, your address bar will not change though a new web page will be loaded. But in case of new URL, you are actually getting a new URL and therefore a new webpage for that URL with address bar updated.

Upvotes: 0

Alasdair
Alasdair

Reputation: 309089

Currently you are rendering the 'home.html template, instead of redirecting the user to the homepage.

If you want to redirect then you should return an HttpResponseRedirect response, or use the redirect shortcut.

from django.shortcuts import redirect

def logout(request):
    ...
    return redirect('/')

Upvotes: 1

Related Questions