validation - Django: how to write code for modelform of user registration -
I want to register form and check that the user's email is new by checking DB.
But the problem is that I do not know how to do it.
I have 2 questions.
1) When there is a validation error in a specific = true stage, how can I catch the error? Override the clean_field () method of the model? And how can I make it?
2) How can I write efficient and good code for this registration app?
The source code below is <
# django.db import model class user (models.Model) from # models.py: university = model. Finish ('university') mail = model. EmailField (Exclusive = True) Password = Model Model = model.Carfild () class university (model.model): name = model.ctrild () mail = model.carfold () to difengo.form import modelform class logonform (modelform): class meta: model = user field = ( 'Mail', 'password') class RegisterForm (ModelForm): class meta: model = user def clean_mail (): data = self.cleaned_data ['mail'] if user.objects.filter (mail = data) .exists () : Increase the form. # Views.py def register (request): If request.method == 'POST': ### If form.is_valid (): < P> I am very confused to do this; thanks in advance I am waiting for help;)
< P> You do not catch the error Django does this for you: When you call form.is_valid () , it calls every clean_file methods and holds a validation error exception Is it Puts in the form. Error feature So, you already have the right
except for a major, huge, absolutely important problem You should never store passwords in plain text Do not create your own standalone user class: inherit the executable user and use the DJ's built-in methods to create a user and use your password. You should not do this.
Comments
Post a Comment