forms.py
from django import forms from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User from django.db import models from django.contrib.auth.models import User from .models import Profile class RegistrationForm(UserCreationForm): # email = forms.EmailField(required=True) # password1 = forms.CharField(widget=forms.PasswordInput) # password2 = forms.CharField(widget=forms.PasswordInput) class Meta: model = Profile fields = ('username', 'password1', 'password2') def clean_password2(self): password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") # self.username.set_password(self.cleaned_data["password1"]) if password1 != password2: raise forms.ValidationError( self.error_messages['password_missamatch'], code='password_mismatch', ) return password2
modals.py
from django.contrib.auth.models import User from django.db import models from django.db.models.signals import post_save from django.dispatch import receiver class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) username = models.CharField(max_length=16) password = models.CharField(max_length=16) location = models.CharField(max_length=30, blank=True) birthdate = models.DateField(null=True, blank=True) email = models.EmailField(unique=True) age = models.CharField(max_length=50, null=True) USERNAME_FIELD = 'username' def __str__(self): return self.user
views.py
from .forms import RegistrationForm def register(request): if request.method == "POST": form = RegistrationForm(request.POST) if form.is_valid(): user = form.save() # user.set_password() # user = auth.authenticate(username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password1')) auth.login(request, user) return HttpResponseRedirect('/profile') else: form = RegistrationForm() return render(request, 'fighter/register.html', {'form': form})