Registration form with profile field

I have a simple question. This is my profile:

Class Profile (models.Model):

user = models.ForeignKey(User, unique=True)
born = models.DateTimeField('born to')    
photo = models.ImageField(upload_to='profile_photo')

I want to create a REGISTRATION FORM with these fields (from user models and profiles):

username, first_name, last_name, birth and photo.

All of these fields are required.

How should I do it?

How does get_profile () work in the template for this problem?

Thank:)

+3
source share
1 answer

Customization

Do you use django-profiles and django-registration projects? If not, you should do this; most of this code has already been written for you.

Profile

Your user profile code:

class Profile(models.Model):
    user = models.ForeignKey(User, unique=True)
    born = models.DateTimeField('born to')    
    photo = models.ImageField(upload_to='profile_photo')

Django? , , yourapp :

AUTH_PROFILE_MODULE = "yourapp.Profile"

django-registration , , . Django , . - . - :

from django import forms
from registration.forms import RegistrationForm
from yourapp.models import Profile
from registration.models import RegistrationProfile

class YourRegistrationForm(RegistrationForm):
    born = forms.DateTimeField()
    photo = forms.ImageField()

    def save(self, profile_callback=None):
        new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
        password=self.cleaned_data['password1'],
        email = self.cleaned_data['email'])
        new_profile = Profile(user=new_user, born=self.cleaned_data['born'], photo=self.cleaned_data['photo'])
        new_profile.save()
        return new_user

django-registration, urls.py:

from registration.backends.default import DefaultBackend
from registration.views import activate
from registration.views import register

# ... the rest of your urls until you find somewhere you want to add ...

url(r'^register/$', register,
    {'form_class' : YourRegistrationForm, 'backend': DefaultBackend},
    name='registration_register'),
+9

Source: https://habr.com/ru/post/1658557/


All Articles