Найти - Пользователи
Полная версия: Получаю такое "'Profile' object has no attribute 'set_password'" в тот момент когда пробую зарегистрировать нового пользователя.
Начало » Django » Получаю такое "'Profile' object has no attribute 'set_password'" в тот момент когда пробую зарегистрировать нового пользователя.
1
mikefromru
Мне нужно сделать регистрацию унаследовав поля от “Profile” in modals.py.
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})       
Почему я получаю “'Profile' object has no attribute ‘set_password’”? Как мне это исправить?
balalay12
Сделайте нормальное расширение модели пользователя https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project и не придется писать ничего лишнего при регистрации.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB