Anyone has an idea why my code is not running on the line 'if user is not None:' onwards?
traceback
1
2
Internal Server Error: /login/ Traceback (most recent call last): File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 137, in _get_response "returned None instead." % (callback.module, view_name) ValueError: The view Account.views.Login didn't return an HttpResponse object. It returned None instead. [18/Jan/2019 21:59:25] "POST /login/ HTTP/1.1" 500 56866
views.py
class Login(View):
form_class = AccountForm
template = 'login.html'
def get(self, request):
form=self.form_class(None)
return render(request, self.template, {'form':form})
def post (self, request):
if request.method=="POST":
form = self.form_class(request.POST)
if form.is_valid():
print('1')
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
print('2')
user = authenticate(username=username, password=password)
if user is not None:
print('3')
if user.is_active:
login(request, user)
return redirect('home.html')
else:
return HttpResponse("Inactive user.")
else:
return render(request, 'login.html')
urls.py
urlpatterns = [
...
path('emailverification/passwordchange/', views.PasswordChange, name='passwordchange'),
]
template
{%extends 'base_form.html'%}
{%block content%}
<div class=container-fluid>
<form method="POST">
{%csrf_token%}
<label for="username">username</label>
<input type="text" placeholder="username" name="login" required><br>
<label for="password">password</label>
<input type="password" placeholder="password" name="login" required><br>
<button type="submit" class="btn btn-success">Submit</button>
</form>
<div class="container signin">
<p>Do not have an account? <a href="{% url 'registration'%}">register here</a>.</p>
<p>Forgot password?<a href="{%url 'passwordreset'%}">retrieve password</a></p>
<p>Improve your password security.<a href="{%url 'passwordchange'%}">change password</a></p>
</div>
<div>
{%endblock content%}
form.is_valid()
holds, butuser is None
, then there is a codepath that does not return anything, hence the error. Note thatif request.method=="POST":
is useless here. – Willem Van Onsem