Reputation: 3806
Following this :
I am having some trouble with this call:
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
In their example I am guessing this works as is on the signup of a new account because the Profile in their example has all fields that can be blank and null. In my case my profile I am trying to maintain here is called:
class APOUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
institution = models.ForeignKey("mainpage.InstitutionMember", on_delete=models.PROTECT)
gender = models.ForeignKey("mainpage.GenderTable", on_delete=models.PROTECT)
on_site_status = models.ForeignKey("mainpage.SiteStatus", on_delete=models.PROTECT)
refer_to_as = models.TextField(max_length = 30, blank=True) #if the above is custom
room_preference = models.ForeignKey("housing.Room", on_delete=models.PROTECT)
Which has references to ultimately what will be drop downs to select form a form (populated by another table with defaults). So do I remove the @reciever and then just have the users fill out the profile separately somehow after they signup and confirm their account?
I tried to mix my signup form with the profile form... but kept getting an anonmyous user object had no attribute object apouser in the views.py when I try to mix the signup forms and profile forms:
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
profile_form = ProfileForm(request.POST, instance=request.user.apouser)
if form.is_valid() and profile_form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
profile_form.save()
current_site = get_current_site(request)
mail_subject = 'Activate your APO account.'
message = render_to_string('acc_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid':urlsafe_base64_encode(force_bytes(user.pk)),
'token':account_activation_token.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return redirect('plsactivate')
#return HttpResponse('Please confirm your email address to complete the registration')
#form.save()
#username = form.cleaned_data.get('username')
#raw_password = form.cleaned_data.get('password1')
#user = authenticate(username=username, password=raw_password)
#login(request, user)
#return redirect('/')
else:
form = SignUpForm()
profile_form = ProfileForm(instance=request.user.apouser) #<-- error here
return render(request, 'signup.html', {'form': form, 'profile_form': profile_form})
#return render(request, 'signup.html', {'form': form})
So not sure the proper way to go about what I need. I have my regular user default with the basic stuff the default django signup comes up with. THen I have the APOUser with extra stuff (usually filled out in a profile) but not sure where/how to get users to fill that out.
On signup gives me the anonymous user error
After signup doesn't let the @reciever work because none of the APOUuser stuff is filled out?
Upvotes: 0
Views: 47
Reputation: 599470
Yes, remove the receiver - although it's not the cause of this specific problem, it will cause issues as soon as you fix that
The reason for your error is as it says: since you don't have a logged in user yet, you don't have a profile either. But you don't need one; there is no need to pass an instance argument there.
What you do need to do is to set the user on the result of saving the profile form - in just the she way as you set is_active on the result of saving the user form.
So:
if request.method == 'POST':
form = SignUpForm(request.POST)
profile_form = ProfileForm(request.POST,)
if form.is_valid() and profile_form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
...
else:
form = SignUpForm()
profile_form = ProfileForm()
...
Upvotes: 1