Reputation: 525
Basically, I'm building a Django application that's like a blog. So, my "social" page is supposed to display posts (that right now I'm adding through the admin page). However, when I load that page, nothing shows up.
Here's my views.py
:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Posts
# Create your views here.
def home(request):
context = {
'posts' : Posts.objects.all()
}
return render(request, 'social/social.html', context)
Here's my models.py
:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Posts(models.Model):
post_title = models.CharField(max_length = 20, help_text = 'Enter post title')
post_text_content = models.TextField(max_length = 1000)
post_author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
post_date = models.DateField(auto_now = True, auto_now_add = False)
### Make seperate model for image content ###
class Meta:
ordering = ['post_title', 'post_author', 'post_date', 'post_text_content']
def __str__(self):
return self.post_title
class Author(models.Model):
first_name = models.CharField(max_length = 100)
last_name = models.CharField(max_length = 100)
date_of_birth = models.DateField(null = True, blank = True)
user_name = models.CharField(max_length = 20)
class Meta:
ordering = ['user_name', 'first_name', 'last_name', 'date_of_birth']
def __str__(self):
return str(f'{self.first_name}, {self.last_name}, {self.user_name}')
Here's my social.html
:
{% extends "social/base.html" %}
{% block content %}
<h1>Your Feed</h1>
<p>This is your feed. Here, you'll see posts from people you follow.</p>
{% for post in Posts %}
<h1>{{post_title}}</h1>
<p>By {{post_author}} on <i>{{post_date}}</i></p>
<p>{{post_text_content}}</p>
{% endfor %}
{% endblock content %}
My base.html
:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
{% if title %}
<title>WebNet - {{title }}</title>
{% else %}
<title>WebNet</title>
{% endif %}
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
Any help would be gladly appreciated. Thank you in advance!
If I need to put any more files in, please let me know.
Upvotes: 0
Views: 48
Reputation: 111
There is a typo as pointed by Brenden. Also to access each value, you have to use the dot(.) operator inside the for loop. Try this:
{% for post in posts %}
<h1>{{ post.post_title }}</h1>
<p>By {{ post.post_author }} on <i>{{ post.post_date }}</i></p>
<p>{{ post.post_text_content }}</p>
{% endfor %}
Upvotes: 2
Reputation: 501
I think the problem is here
{% for post in Posts %}
I think it should be
{% for post in posts %}
Upvotes: 2