So I created a user profile by following this tutorial https://www.oodlestechnologies.com/blogs/How-to-Edit-User-Profile-Both-Django-User-and-Custom-User-Fields/ but every time I go the profile page when not logged in it shows that I am logged in I don't know if django thinks im the user that the profile im viewing.
I tested it by adding this into the base.html
<p>{% if user.is_authenticated %}
You are logged in
{% endif %}</p>
It doesn't show it on any pages because im not logged in unless I go to view someones profile then it says im logged in which I am not
models.py
class UserProfileManager(models.Manager):
pass
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
avatar = models.ImageField(upload_to='avatars', blank=True)
location = models.CharField(max_length=100, default='', blank=True)
date_of_birth = models.DateField(default='00/00/0000', null=True, blank=True)
website = models.URLField(default='', blank=True)
bio = models.TextField(default='', blank=True)
def __str__(self):
return self.user.username
def age(self):
dob = self.date_of_birth
tod = datetime.date.today()
my_age = (tod.year - dob.year) - int((tod.month, tod.day) < (dob.month, dob.day))
return my_age
def createProfile(sender, **kwargs):
if kwargs['created']:
user_profile = UserProfile.objects.created(user=kwargs['instance'])
post_save.connect(createProfile, sender=User)
views.py
def public_profile_view(request, username):
user = User.objects.get(username=username)
userprofile = UserProfile.objects.all()
# date_joined = request.user.date_joined
# last_login = request.user.last_login
context = {
'user': user,
'userprofile': userprofile,
# 'date_joined': date_joined,
# 'last_login': last_login,
}
return render(request, "/account/profile/public_profile.html", context)
def profile_edit_view(request):
userprofile = UserProfile.objects.all()
if request.method == 'POST':
form = EditProfileForm(request.POST, instance=request.user)
profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.userprofile)
if form.is_valid() and profile_form.is_valid():
user_form = form.save()
custom_form = profile_form.save(False)
custom_form.user = user_form
custom_form.save()
return redirect('profile_urls:public_profile')
else:
form = EditProfileForm(instance=request.user)
profile_form = ProfileForm(instance=request.user.userprofile)
args = {'userprofile': userprofile,}
# args.update(csrf(request))
args['form'] = form
args['profile_form'] = profile_form
return render(request, "/account/profile/profile_edit.html", args)
forms.py
class EditProfileForm(ModelForm):
class Meta:
model = User
fields = ('email', 'first_name', 'last_name')
class ProfileForm(ModelForm):
date_of_birth = forms.DateField(label='Date of Birth')
class Meta:
model = UserProfile
fields = ('avatar', 'location', 'website', 'date_of_birth', 'bio', 'website')
def clean(self):
cleaned_data = super().clean()
location = cleaned_data.get('location')
website = cleaned_data.get('website')
date_of_birth = cleaned_data.get('date_of_birth')
bio = cleaned_data.get('bio')
website = cleaned_data.get('website')
it is happending because you are passing user object in public_profile.html
by doing following
def public_profile_view(request, username):
user = User.objects.get(username=username)
to check wheather actual user is logged in or not use request.user
<p>{% if request.user.is_authenticated %}
You are logged in
{% endif %}</p>