Not showing bio tag in Django?
Question:
I’m working on a blog page, and I’m creating page about authors but it doesn’t work to load bio (short description). I don’t know why it wasn’t working, please see code below:
this is author_detail.html
<p>Bio: {{ author.bio }}</p>
this is views.py
def author_detail(request, slug):
user = User.objects.get(username=slug)
author = Author.objects.get(user=user)
posts = Post.objects.filter(author=user)
return render(request, 'author_detail.html', {'author': user, 'posts': posts})
this is models.py
class Author(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True, default='', help_text='Enter a short description about yourself.')
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = self.user.username.lower().replace(' ', '-')
super().save(*args, **kwargs)
if not self.bio:
self.bio = "This author hasn't added a bio yet."
def __str__(self):
return self.user.username
if I add print('author.bio')
it gives me in the terminal the bio, but I don’t know why it doesn’t show up in (html) page… Any idea?
Answers:
As already described by kamran890 in the above comment.
It should be author
in context
not user
so:
def author_detail(request, slug):
user = User.objects.get(username=slug)
author = Author.objects.get(user=user)
posts = Post.objects.filter(author=user)
return render(request, 'author_detail.html', {'author': author, 'posts': posts})
If you want to retrieve first_name
from author
instance then use {{author.user.first_name}}
.
I’m working on a blog page, and I’m creating page about authors but it doesn’t work to load bio (short description). I don’t know why it wasn’t working, please see code below:
this is author_detail.html
<p>Bio: {{ author.bio }}</p>
this is views.py
def author_detail(request, slug):
user = User.objects.get(username=slug)
author = Author.objects.get(user=user)
posts = Post.objects.filter(author=user)
return render(request, 'author_detail.html', {'author': user, 'posts': posts})
this is models.py
class Author(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True, default='', help_text='Enter a short description about yourself.')
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = self.user.username.lower().replace(' ', '-')
super().save(*args, **kwargs)
if not self.bio:
self.bio = "This author hasn't added a bio yet."
def __str__(self):
return self.user.username
if I add print('author.bio')
it gives me in the terminal the bio, but I don’t know why it doesn’t show up in (html) page… Any idea?
As already described by kamran890 in the above comment.
It should be author
in context
not user
so:
def author_detail(request, slug):
user = User.objects.get(username=slug)
author = Author.objects.get(user=user)
posts = Post.objects.filter(author=user)
return render(request, 'author_detail.html', {'author': author, 'posts': posts})
If you want to retrieve first_name
from author
instance then use {{author.user.first_name}}
.