Q:

post profile information in django extended user model (user has no profile)

I know that this is a frequent topic, but however, with all the resources available on the web and stackoverflow, I couldn't get my form to work properly.
I get this error : Exception Type: RelatedObjectDoesNotExist, Exception Value: 
User has no profile.
Here is my setup in Django.
models.py
from django.db import models
from django.contrib.auth.models import User
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, related_name='profile')
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)

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

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if kwargs.get('created', False):
         Profile.objects.create(user=kwargs['instance'])
post_save.connect(create_user_profile,sender=User)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, created,**kwargs):
    if created:
        instance.profile.save()

forms.py
from django.contrib.auth.models import User
from django import forms
from django.contrib.auth.forms import UserCreationForm

class SignUpForm(UserCreationForm):
    birth_date = forms.DateField()
    location = forms.CharField()
    password1 = forms.CharField(label=("Password"), widget=forms.PasswordInput) 
    password2 = forms.CharField(label=("Confirm password"), widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'location','email', 'birth_date')
        widgets = {
            'birth_date': forms.DateInput(attrs={'class':'datepicker'}),
        }
        labels = {
            'username': ('Capser name'),
        }
        help_texts = {
            'username' : None,
            'birth_date': None,
        }

views.py
class UserFormView(View):
    form_class = SignUpForm
    template_name = 'home/registration_form.html'

    #display a blank form
    def get(self, request):
        form = self.form_class(None)
        return render (request, self.template_name, {'form': form})

    #process form data
    def post(self, request):
        form = self.form_class(request.POST)

        if form.is_valid():
            user = form.save(commit=False)

            #user.refresh_from_db()  # load the profile instance created by the signal
            password = form.cleaned_data['password1']
            user.set_password(password)
            username = form.cleaned_data['username']
            user.profile.birth_date = form.cleaned_data.get('birth_date')
            user.profile.location = form.cleaned_data.get('location')    
            user.save()

            #return user objects if credentials are correct
            user = authenticate(username=username, password=password)

            if user is not None:
                if user.is_active:
                    login(request, user)
                    return redirect('home:home')

        return render (request, self.template_name, {'form': form})

And if I uncomment the commented lins user.refresh_from_db(), i get the following error : Exception Type:    DoesNotExist, Exception Value:
User matching query does not exist.
I'm not sure at all, but I suspect that the signals in the models.py are not working properly.
Anyone could help ?

A:

First of all, you can't assign: user.profile, because user is an instance of the model User, which does not have this attribute.
I would not recommend this structure that you are using to create profile after creating a User using signals. You'd better create the User with its attributes (username, password, email) and then create the Profile instance.
user.save()
Profile.objects.create(
    user=user,
    location=form.cleaned_data.get('location'),
    birth_date=form.cleaned_data.get('birth_date'))

If you still want to use signals to this task, you should search for "How to pass arguments by signals". So, you would pass location and birth_date by a dictionary and use this data to create a Profile.

