0

I am facing a problem while building a Django web app. I want that if a user logs into his account, his session should be stored and when he agains visits the login page ,he should be redirected to his home page. I have tried using

Here is my code.

Views.py

from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.template import RequestContext

def index(request):
    return HttpResponse("Index Page")

@login_required
def home(request):
    ctx = {}
    return render_to_response('auth/home.html',ctx, context_instance = RequestContext(request))

Urls.py

from django.conf.urls.defaults import *
from django.contrib.auth.views import login, logout
urlpatterns = patterns('',
url(r'^$','apps.auth.views.index'),

)


urlpatterns = patterns('',
url(r'cc/', login, kwargs = {'template_name' : 'auth/cc.html'} , name = 'cc_login'),
url(r'logout/', logout, name = 'cc_logout'),
url(r'home/','apps.auth.views.home', name = 'cc_home'),
)
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267

2 Answers2

2

I ran into the same situation with my django project.

I solved it by making a view that was similar too:

def login_page(request):
    if request.user.is_authenticated():
        return redirect(<insert your desired page here>)
    else:
        return render(<whatever the login page is>)

This way, if the user is logged in, they will be redirected to whatever page you want them to be.

EDIT:

In response to the comments below, I am modifying my answer (original is still above). Here is what I have done to solve your problem.

from django.contrib.auth.views import login

def custom_login(request, **kwargs):
    """
        Redirects the user to the desired page if they are authenticated
    """
    if request.user.is_authenticated():
        return redirect(<whatever page or view you want them to go to>)
    else:
        return login(request, **kwargs)

For example, your kwargs could be {'template_name': 'your_login_template.html', 'authentication_form': YourCustomRegistrationFormClass}

If you're using the standard login and registration procedures, just leave the kwargs part blank.

buched
  • 36
  • 5
  • Thanks buched but I am not able to render my template when passed as a parameter. The template contains inbuilt django form {{form.as_p}}. It does not views that. Any suggestions? – Praful Bagai Jul 11 '13 at 21:51
  • I'm not sure I understand your question exactly, but your problem might be caused by not calling the render function properly. you can find an explanation here https://docs.djangoproject.com/en/dev/topics/http/shortcuts/. To call the render function, you will need to pass in the proper parameters (e.g. render(request, 'your_template.html', {dictionary of context variables}). – buched Jul 12 '13 at 00:09
  • To clarify further, the 'form' for the 'form.as_p' that you use in the render context dictionary should be passed in like this render(request, 'your_template.html', {'form': your_form}) – buched Jul 12 '13 at 00:29
  • Thanks buched. I have used Django's inbuilt form, ie 'form.as_p'. So, what exactly should goes into {'form': your_form}. – Praful Bagai Jul 12 '13 at 06:16
  • PS: I have not made any form for login, rather I have used the inbuilt form. Replacing 'your_form' with 'form.as_p' does not works. – Praful Bagai Jul 12 '13 at 06:18
  • I have modified the answer above to try and answer your question. – buched Jul 14 '13 at 01:02
  • If you're using the standard login view, don't worry about the form.as_p thing. By redirecting to django's builtin login view, the form variable will be passed into the template you specify and should work. – buched Jul 14 '13 at 01:08
0

I'm fairly new to Django, but have you tried overriding the default login view?

views.py

from django.contrib.auth import views as auth_views
from django.shortcuts import redirect

def login(request, *args, **kwargs):
    if request.method == 'POST':
        request.session.set_expiry(0) # Remember user session
    if request.user.is_authenticated():
        return redirect(USER_PAGE_ADDRESS)
    return auth_views.login(request, *args, **kwargs)

urls.py

urlpatterns = patterns('',
    url(r'cc/', APP_NAME.views.login, kwargs = {'template_name' : 'auth/cc.html'} , name = 'cc_login'),
    ...
)
chipperdrew
  • 403
  • 4
  • 11
  • 1
    @user1162512 sorry I don't have enough points to respond to buched post, but have you tried passing your login function additional arguments (like *args and **kwargs) as I did in my answer above? Then it would know to expect the template argument. – chipperdrew Jul 13 '13 at 00:13