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'),
)
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'),
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.