0

here is the form

class LoginForm(forms.Form):
    email = forms.EmailField(
        label=("Email"),
        widget=forms.TextInput(attrs={
            'id': 'inputEmail',
            'class': 'form-control inp',
            'type': 'text',
            'placeholder': 'your email'
        }),
        required=True
    )
    password = forms.CharField(
        widget=forms.PasswordInput(
            render_value=False,
            attrs={
                'id': 'inputPassword',
                'class': 'form-control inp',
                'type': 'password',
                'placeholder': 'Password'
            }
        ),
        required=True
    )

and here is the view for the form

class Home(View):
    template_name = "sellers/seller_home.html"
    form_class = LoginForm


    def get(self, request , *args , **kwargs):
        context = {}

        login_form = LoginForm()

        context['login_form'] =  login_form

        return render(request , "sellers/seller_home.html" , context)

    def post(self , request , *args , **kwargs):
        email = request.POST.get('email')
        name = request.POST.get('store_name')
        password = request.POST.get('password')
        mobile = request.POST.get('mobile')

        form = self.form_class(request.POST)


        if request.is_ajax():
            if email:
                if User.objects.filter(email=email).exists():
                    return HttpResponse(json.dumps('True'))
                else:
                    return HttpResponse(json.dumps('False'))
            if mobile:
                if User.objects.filter(mobile=mobile).exists():
                    return HttpResponse(json.dumps('True'))
                else:
                    return HttpResponse(json.dumps('False'))
        else:
            if form.is_valid():
                email = form.cleaned_data["email"]
                password = form.cleaned_data["password"]

                user = User.objects.get(email__iexact=email)

                authenticated_user = authenticate(username=user.username , password=password)

                if not authenticated_user:
                    raise ValidationError("Email or password is incorrect")
                else:
                    login(request , authenticated_user)
                    return HttpResponseRedirect(reverse('dashboard'))
            else:
                return render(request , self.template_name , {'login_form' : form})

the problem here iam facing is that i am not able to show to validation error as a label in the template .it throws error as validation error .how can i achieve the validation in a better way?

here is the traceback

Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in dispatch
  89.         return handler(request, *args, **kwargs)
File "C:\Users\lenovo\Desktop\Grooved2\grooved\src\sellers\views.py" in post
  82.                   raise ValidationError("Email or password is incorrect")

Exception Type: ValidationError at /sellers/
Exception Value: [u'Email or password is incorrect']

2 Answers 2

4

Django views do not handle validation errors. You should raise ValidationError in the form's clean_<field> or clean methods.

Note that Django already has a built in AuthenticationForm that you could use. If you look at its source code, you can see how it calls authenticate in the form's clean method, not in the view.

Sign up to request clarification or add additional context in comments.

Comments

2

You are use built-in authenticate method:

authenticated_user = authenticate(username=user.username , password=password) so no need to raise ValidationError

Django views does not handle it. You should use clean method for validate the fields.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.