Hey guys. I have been struggling with an issue for a couple of days and I think the answer may be to create a custom User model and custom Manager but would like to see if I can avoid that for now. What I am trying to do is register a user and have them fill out some profile information at the same. I keep running into the error ‘‘AnonymousUser’ object has no attribute ‘_meta’’ whenever I am not logged in as Admin. I am confused why I would need to be logged in to create a new account, hence why I am here.
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)
branch = models.CharField(max_length=50, blank=True)
region = models.CharField(max_length=30, blank=True)
# birth_date = models.DateField(null=True, blank=True)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
# print("create",sender, instance,created,**kwargs)
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
# print("save",sender, instance,**kwargs)
instance.profile.save()
Forms.py
from .models import EmployeeDifferentLocation, Profile
from django.contrib.auth.models import User
# create a ModelForm
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email')
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = '__all__'
exclude = ['user']
Views.py
from django.shortcuts import render, redirect
from .forms import EmployeeLocation, UserForm, ProfileForm
from .models import EmployeeDifferentLocation
from .filters import EmployeeFilter
from django.contrib.auth.decorators import login_required
def create_profile(request):
print(request)
if request.method == "POST":
user_form = UserForm(request.POST, instance=request.user)
profile_form = ProfileForm(request.POST, instance=request.user.profile)
if user_form.is_valid() and profile_form.is_valid():
user =user_form.save(commit=False)
profile = profile_form.save()
profile.user = user
profile.save()
user.save()
# messages.success(request,('Your profile was successfully updated!'))
return render(request,'home.html')
user_form = UserForm(instance=request.user)
profile_form = ProfileForm(instance=request.user.profile)
return render(request, 'profile.html',{'user_form':user_form, 'profile_form':profile_form})
I am wanting a user to fill out what their ‘Region’ and ‘Branch’ would be as they are registering their new user account is my end goal. I have watched videos on how to create custom user models and managers and may just have to go that route. Can someone explain to me why I am needing to be logged in to register a new account?
Any help would be greatly appreciated. Thanks!