D
D
DenNewGit2018-03-23 12:26:04
Django
DenNewGit, 2018-03-23 12:26:04

How to register in Djnago?

I have this code:

def register(request):
    if request.method =='POST':
        form = RegistrationForm(request.POST)

        if form.is_valid():
            new = form.save(commit=False)
            new.save()
            # return redirect(reverse('accounts:home'))
            return redirect('/home/')
    else:
        form = RegistrationForm()
        args = {
            'form': form,
        }
        return render(request, 'accounts/reg_form.html', args)

class RegistrationForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = (
            'username',
            'first_name',
            'last_name',
            'email',
            'password1',
            'password2',

        )

    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.email = self.cleaned_data['email']

        if commit:
            user.save()

        return user

But at the same time, there is also a UserProfile model, and you need to make sure that the user fills it out during registration.
class UserProfile(models.Model):
    user = models.OneToOneField(User)
    description = models.CharField(max_length=100, default='')
    city = models.CharField(max_length=100, default='')
    website = models.URLField(default='')
    phone = models.IntegerField(default=0)
    image = models.ImageField(upload_to='profile_image', blank=True)


    london = UserProfileManager()

    def __str__(self):
        return self.user.username

How to do it? Now only fields from
from django.contrib.auth.models import User are displayed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2018-03-23
@deliro

Well, here it is, in black and white.
https://docs.djangoproject.com/en/2.0/topics/auth/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question