I have been trying to figure out how all this validation works, but I am not getting the hang of it. I read the very few examples on djangoproject, but I am missing concepts and how everything is tied together.
If you could please look at my code and rearrange how things should be, as well as a few explanations, that would be awesome!
So I want to do something very simple: have a login from with email ONLY. When a user types their email, i want to check if it's in the database, and if it is, login. if it is not, i want to raise an error 'user already in database' and suggest that this person goes to /register
So what i currently have is:
view.py:
def emailLogin(request, backend, extra_context=None, initial={}):
form = EmailLoginForm(initial=initial)
if request.method == 'POST':
form = EmailLoginForm(initial=initial, data=request.POST)
if form.is_valid():
user = form.do_save()
_no_pass_login(request, user) # my custom login
return redirect('/')
else:
print ('not valid')
return render_jinja(request, 'registration/email_login_form.html',
type="register",
form = form
)
forms.py:
class EmailLoginForm(forms.Form):
email = forms.EmailField()
def do_save(self):
try:
u = User.objects.get(email=self.cleaned_data['email'])
except :
raise forms.ValidationError("Already in DB")
return u
So the whole point is that I am missing concepts - where should a validation error be raised, the view or the form? where is it raised to? who catches it? what needs to be imported in each file etc.
This shouldn't be too difficult but i am totally lost now, and i have no examples I can analyze and mod my code to work, so i am here.