0

I want to authenticate a user such that when he logs into his account and then wants to go back to the login page, he/she should be automatically redirected to the dashboard page. How can I do that?

@login_required @csrf_exempt def dashboard(request): users = GrabhaloUser.objects.exclude(user_id = request.user.id) if request.is_ajax(): if request.POST.has_key('message'): selected_users = request.POST.getlist('selected_users[]') message = request.POST['message'] send_query(request,selected_users,message) ctx = { 'users' : users } return render_to_response('dashboard/dashboard.html',ctx, context_instance = RequestContext(request)) 

login URLS

urlpatterns = patterns('', url(r'login/',login,kwargs = {'template_name' : 'auth/login.html'}, name = 'grabhalo_login'), url(r'logout/', logout,kwargs = {'template_name' : 'auth/logout.html'}, name = 'grabhalo_logout'), url(r'register/','apps.auth.views.register', name = 'grabhalo_register'), ) 

2 Answers 2

1

Make a function login_page , check the authentication of the user there, if authenticated, redirect it to dashboard, else return to the login page. Map this function to the login url in urls.py

def login_page(request): if request.user.is_authenticated(): return redirect('/dashboard/') else: return login(request) 

And then map this function to the login url.

url(r'login', 'modules.energy.login.views.login_page', name = 'cilantro_login'), 
Sign up to request clarification or add additional context in comments.

Comments

0

You may try this:

Whenever the user clicks the login page link, the view for the login page is executed. In that view, check if the user is logged in. If the user is logged in,, then redirect him to the dashboard, else display the login page. It is as simple as it is. The sample code:

if request.user.is_authenticated(): #load dashboard else: #load login page 

5 Comments

I knew about this code. But the problem is that I'm using the Django's inbuilt login view. I've not customized it. So where do I use this code? Inside the same inbuilt code or should I customize and built my own??
Adding a login view doesn't cost you much i believe
But I personally believe that its good to use the Django's inbuilt functionality which will be more efficient than me building my own? Isnt it? Open for discussion.
stackoverflow.com/questions/13440298/django-how-to-use-built-in-login-view-with-email-instead-of-username This tells the same :-)
@prafulbagai: Actually using the inbuilt-view is better if you want to use it as it is. Since you want to use it with some extra features, then you have to override the inbuilt one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.