0
0 Комментариев
class RegistrationView(View):
  template_name = 'regestration.html'
  def get (self, request, *args, **kwargs):
  form = RegistrationForm()
   context = {
   'form': form
  }
  return render(self.request, self.template_name, context)
  def post(self, request, *args, **kwargs):
  form = RegistrationForm(request.POST or None)
   if form.is_valid():
   new_user = form.save(commit=False)
   username = form.cleaned_data['username']
   password = form.cleaned_data['password']
   new_user.set_password(password)
   password_check = form.cleaned_data['password_check']
   first_name = form.cleaned_data['first_name']
   last_name = form.cleaned_data['last_name']
   email = form.cleaned_data['email']
   new_user.save()
   UserAccount.objects.create(user=User.objects.get(username=new_user.username),
          first_name=new_user.first_name,
          last_name=new_user.last_name,
          email=new_user.email)
     return HttpResponseRedirect('/')
  context = {
   'from': form
  }
  return render(self.request, self.template_name, context)
forms.py
class RegistrationForm(forms.ModelForm):
 password_check = forms.CharField(widget=forms.PasswordInput)
 password = forms.CharField(widget=forms.PasswordInput)
      class Meta:
  model = User
  fields = [
   'username',
   'password',
   'password_check',
   'first_name',
   'last_name',
   'email',
   ]
  def __init__(self, *args, **kwargs):
   super(RegistrationForm,self).__init__(*args, **kwargs)
  self.fields['username'].label = 'Логин'
  self.fields['username'].help_text = 'Обязательное поле'
  self.fields['password'].label = 'Пароль'
  self.fields['password_check'].label = 'Подтвердите пароль'
  self.fields['first_name'].label = 'Имя'
  self.fields['last_name'].label = 'Фамилия'
  self.fields['email'].label = 'Email'
    def clean(self):
     username = self.cleaned_data['username']
  password = self.cleaned_data['password']
  password_check = self.cleaned_data['password_check']
   if User.objects.filter(username=username).exists():
   raise forms.ValidationError('Имя пользователя занято!')
   if password != password_check:
   raise forms.ValidationError('Пароли не совпадают!')

выдает ошибку

 if form.is_valid():

username = self.cleaned_data[‘username’]

KeyError: ‘username


Добавить комментарий