0
votes

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%}
2
If the form.is_valid() holds, but user is None, then there is a codepath that does not return anything, hence the error. Note that if request.method=="POST": is useless here.Willem Van Onsem
Took me awhile to figure out my problem, but your comment pointed me in the right direction. Thanks!HAN YAO FOONG

2 Answers

0
votes

You passed a template name ´home.html´ to the redirect function. You should pass a URL instead. Apart from url, you some other options for the parameter. See doc

0
votes

added code

def clean(self, *args, **kwargs):
    username=self.cleaned_data.get("username")
    password=self.cleaned_data.get("password")
    user=authenticate(username=username, password=password)
    if not user:
        raise forms.ValidationError("This user does not exist")
    if not user.check_password(password):
        raise forms.ValidationError("Incorrect Password")
    if not user.is_active:
        raise forms.ValidationError("This user is no longer active.")
    return super(LoginForm, self).clean(*args, **kwargs)