Reputation: 922
I am using Django 1.8.6 and python 3.6. I am quite new to django and python, trying to make a blog. I am not able to retrieve the data from database. Even though it is there in database
my model.py
class Post(models.Model):
STATUS_CHOICES = (('draft', 'Draft'), ('published','Published'), )
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='publish')
author = models.ForeignKey(
User,
blank=True,
null=True, )
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(
max_length=10, choices=STATUS_CHOICES, default='draft')
objects = models.Manager() # The default manager
published = PublishedManager() # custom manager
class Meta:
ordering = ('-publish', )
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse(
'blog:post_detail',
args=[
self.publish.year, self.publish.strftime('%m'),
self.publish.strftime('%d'), self.slug
])
view which handles this model is
def post_detail(request, year, month, day, post):
post = get_list_or_404(
Post,
slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
return render(request, 'blog/post/detail.html', {'post': post})
when us the html template for getting data
<p>Published {{ post.pulish }} by {{ post.author }}</p>
The output is just " Published by"
But when I use just
{{ posts }}
I getting output
[<Post: this is Me>]
"this is Me " is the title of post saved in database
Thanks in advance for your time and effort.It would be great help if you inform me what the problem is.
Upvotes: 0
Views: 132
Reputation: 53774
your HTML template needs to iterate through the result set (even if there is only a single post)
{% for post in posts %}
<p>Published {{ post.publish }} by {{ post.author }}</p>
{% endfor %}
And it should be publish
instead of pulish
Or if you don't want to iterate through result set you can do
<p>Published {{ post.0.publish }} by {{ post.0.author }}</p>
where 0
is the index of the first record.
This is because databases always return what's known as a resultset - a collection of rows regardless of what programming language you use. So this approach is needed.
Upvotes: 3
Reputation: 1338
In your view you getting the list of Post
objects so you should iterrate through the list of posts in the template like so
{% for post in posts %}
< your stuff here >
{% endfor %}
if you don't want to iterrate through the list of Post
objects you should either get one Post
object in your view:
post = get_object_or_404(
Post,
slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
or in your template get object by index:
<p>Published {{ post.0.publish }} by {{ post.0.author }}</p>
where 0
is the index of the record
Upvotes: 2
Reputation: 143
Did you wrote loop in code
you should use for loop for retrieving data regardless of how many entries in your db
{% for post in posts %}
< your stuff here >
{% endfor %}
Upvotes: 0