python - unbound method is_valid() must be called with UserLoginForm instance as first argument (got nothing instead) -
hello guys idea solve error, using django framework develop simple application using class based views.
here view codes:
class openlogin(formview): template_name = 'signin.html' form_class = userloginform def post(self, request, *args, **kwargs): form = self.form_class next_url = self.request.get.get('next') if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') obj = authenticate(username=username, password=password) if obj not none: if obj.is_active(): login(self.request, obj) if next_url not none: return httpresponseredirect(next_url) else: return httpresponseredirect(reverse('index')) else: messages.error(self.request, 'error! sorry account not active') return httpresponseredirect(reverse('signin')) else: messages.error(self.request, 'error!, sorry account not found') return httpresponseredirect(reverse('signin')) else: messages.error(self.request, 'sorry! error occurred, try again') context = { 'title': 'sign in', 'next_url': next_url, 'messages': messages.get_messages(self.request), 'user': self.request.user, 'form':form, } return context
and here form class:
class userloginform(forms.form): username = forms.charfield(max_length=user._meta.get_field('email').max_length) password = forms.charfield(min_length=6, max_length=16, widget=forms.passwordinput()) def is_valid(self): valid = super(userloginform, self).is_valid() if not valid: return valid try: user = user.objects.get( q(username=self.cleaned_data['username']) | q(email=self.cleaned_data['username']) ) except user.doesnotexist: self._errors['no_user'] = 'user not exist' return false if not check_password(self.cleaned_data['password'], user.password): self._errors['invalid_password'] = 'password invalid' return false return true
after submitting form end following error:
unbound method is_valid() must called userloginform instance first argument (got nothing instead)
where mistake? out
you have couple of problems here, 1 that's causing immediate error:
form = self.form_class ... if form.is_valid():
self.form_class
reference class, not form instance. should construct form instance based on request, , validate that:
form = self.form_class(request.post)
wiki
Comments
Post a Comment