N
N
Neoliz2016-04-28 22:12:12
Django
Neoliz, 2016-04-28 22:12:12

Django how to create user with extended model?

Good evening! The question is in the header.
I made a model according to the guide: dunmaksim.blogspot.ru/2015/05/django-18.html
And there is a question. I'm just learning django yet.
The entire Internet has been rummaged.
I was able to login via registration redux
and now.
Please show the code, preferably, how do I register users?
Those. with the usual registration form. And yet, I do not want the activation letter to arrive in the mail. Can it be removed?
Thank you very much

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Neoliz, 2016-04-28
@timofeydeys

Here is my code if anyone is interested
in forms.py

class UserCreationForm(forms.ModelForm):
    password1 = forms.CharField(
        label='passwd',
        widget=forms.PasswordInput(attrs={
            'style':'margin:10px; padding:10px;height:40px',
            'class':'form-control col-sm-8',
                'placeholder': 'Password please'
        })
    )
    password2 = forms.CharField(
        label='double passwd',
        widget=forms.PasswordInput(attrs={
            'style':'margin:10px; padding:10px;height:40px',
            'class':'form-control col-sm-8',
                'placeholder': 'Double password please'
        })
    )

    def clean_password2(self):
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError('Passwd and double passwd error')
        return password2


    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data['password1']) # важно тут передавать password1 или 2. Но не password как в примере выше
        if commit:
            user.save()
        return user
    

    class Meta:
        model = get_user_model()
        fields = ('email', 'lastname', 'firstname')
        widgets = {
            'email': forms.TextInput(attrs={
                'style':'margin:10px; padding:10px;height:40px',
                'class':'form-control col-sm-8',
                'placeholder': 'E-mail please'
            }),
            'lastname': forms.TextInput(attrs={
                'style':'margin:10px; padding:10px;height:40px',
                'class':'form-control col-sm-8',
                'placeholder': 'Lastname please'
            }),
            'firstname': forms.TextInput(attrs={
                'style':'margin:10px; padding:10px;height:40px',
                'class':'form-control col-sm-8',
                'placeholder': 'Firstname please'
            }),
        }

Here is the view:
def registerPost(request):
    if request.method == "POST":
        user_form = UserCreationForm(request.POST)
        
        if user_form.is_valid():
            new_user = user_form.save(commit=False)
            new_user.set_password(user_form.cleaned_data['password1'])
            new_user.avatar = "fotos/no_image_dummy.png"
            new_user.save()
            return HttpResponseRedirect("/login")
    else:
        user_form = UserCreationForm()
    return render(request, 'register.html', {'form':user_form})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question