Get data from Profile model


I have following issue with getting data from my Profile model.

I have created Profile model which have first_name, last_name, BIO and social_facebook fields.

class Profile(models.Model):
	first_name = models.CharField(max_length=60)
	last_name = models.CharField(max_length=60)
	BIO = models.TextField(max_length=800, blank=True, null=True)
	social_facebook = models.CharField(max_length=100, blank=True, null=True, verbose_name='Facebook')

I want insert social_facebook value (link) in footer.html template, for that I created file and inserted code there

def profile_context_processor(request):
	item = Profile.objects.all()
	return {
		'soc_fb': item,

and in footer.html I inserted: {{soc_fb}}

I know that the profile_context_processor function is not valid. It gives me the name of Profile which I created in Django Admin panel. Can you help me to get information correctly?

I think your first need to connect Profile to the User object with one-to-one relationship (One-to-one relationships | Django documentation | Django)

Then in your profile_context_processor you could access the user property on request and find existing Profile instance.

Something like this:

def profile_context_processor(request):
	profile = Profile.objects.filter(user=request.user).first()
	return {
		'soc_fb': profile.social_facebook,
1 Like