I can’t figure out why the latest_blogs_list continues to print as blank even though I just added a post to the database, hence making the test return false. As far as I can tell, my syntax is flawless, I’ve gone over it with several fine-toothed combs and found nothing wrong. it just returns empty.
I checked that the post is being created, and it’s definitely there, but it’s not being added to the list.
# tests.py
def test_latest_blogs_list_with_one_post(self):
response = self.client.get(reverse(index))
User.objects.create(password="12345", is_superuser=False, is_staff=False, is_active=True,username="testuser", first_name="test", last_name="user",
email="guineveremayberry@gmail.com", date_joined=datetime.now())
post_author = User.objects.get(username="testuser")
post_content = "This is a test. This is only a test. This test will tell us whether or not latest_blogs_list will display on the home page when there is only one post."
post1 = Post.objects.create(title="test1", slug="test1", author=post_author, content=post_content, status=1)
print(response.context['latest_blogs_list'])
self.assertEqual(response.status_code, 200)
self.assertContains(response, post1.title)
self.assertQuerysetEqual(response.context['latest_blogs_list'], [post1])
<!--base.html which is extended by index.html-->
<h4>Recent Blogs</h4>
{% if latest_blogs_list %}
{% for post in latest_blogs_list %}
<p><a href="">{{ post.title }}</a></p>
{% endfor %}
{% else %}
<p>No posts are available.</p>
{% endif %}
# models.py
STATUS = (
(0, "Draft"),
(1, "Publish")
)
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200,unique=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
updated_on = models.DateTimeField(auto_now=True)
content = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title