I'm new to django and getting stuck on what I think is a simple issue. I've set up user authentication in my app which is working fine, the problem I'm having is with the url address that appears when my form is submitted. Here is the problem:
The action on my form that takes in a user's login credentials is set to "{% url 'home' %}"
which is a reference to /dashboard/home
. This is exactly what I want the url to be in the event of a successful login. However, when the login is unsuccessful, I want to stay on the login url, which is /dashboard/login
. As you can imagine, what's happening instead is that in the event of an unsuccessful login, the url is still changing to /dashboard/home
. I think this is misleading for the user and generally just bad practice, but I don't know how to dynamically change the url depending on the result of the login attempt. How can I change the url to /dashboard/login
when the attempt is unsuccessful but keep it as /dashboard/home
when it is successful?
My files
HTML template
Truncated to just the form.
<form method="POST" action="{% url 'home' %}">
{% csrf_token %}
<div class="form">
<label for="email" class="credentials">Email:</label>
<input type="text" name="email" id="email" size="12" maxlength="30" />
</div>
<div class="form">
<label for="password" class="credentials">Password:</label>
<input type="password" name="password" id="password" size="12"
maxlength="30" />
</div>
<div class="submit">
<input type="submit" name="submit" value="GET STARTED" />
</div>
</form>
Views
Excuse all of the print functions, they're a help to a beginner :) I also played with a couple of variables I passed to my template via a context dictionary that I haven't removed from the views yet (e.g.,"login_successful")
def portal(request):
if request.method == 'POST':
print ("first if was executed")
form = AuthenticationForm(request.POST)
if form.is_valid():
print ("second if was executed")
username = request.POST['email']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
print("user authenticated")
if user.is_active:
print("user active")
login_successful = 1
print('login_successful: ',login_successful)
login(request, user)
d = (UserFields.objects.filter(userid__exact=username).values())
d2 = d[0]
print(d2)
m = d2["manager"]
am = d2["apexmanager"]
print (m)
print (am)
if am==True:
print ("apex executed")
return render(request, 'dashboard/homeApexManager_new.html', {'login_error': login_successful})
elif m==True:
print ("m true executed")
return render(request, 'dashboard/homeManager_new.html', {'login_error': login_successful})
else:
print ("m false executed")
return render(request, 'dashboard/homeEmployee_new.html', {'login_error': login_successful})
else:
print("not active")
return render(request, 'dashboard/login.html')
else:
print ("not authenticated")
login_error = 1
print (login_error)
return render_to_response('dashboard/login.html', {'login_error': login_error})
else:
print ("form is not valid")
login_error=1
print (login_error)
return render_to_response('dashboard/login.html', {'login_error': login_error})
else:
print ("request is not POST")
return render(request, 'dashboard/login.html', {'form': form})
Urls
from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^login', auth_views.login,name = 'login'),
url(r'^home', views.portal,name = 'home'),
]
As AMG mentions in his comment, the RightThing(tm) to do is to handle the form submission (POST) in your login view and then either redirect to the dashbord home url (if the form is valid and the login succeeds) or redisplay the form (without redirection) if the form is invalid or the login fails. As a general rule here's the canonical way to handle form submissions (except for search forms which should use the GET method and never redirect):