Reputation: 1
I have verified that there are entries in the Books table, but my template is not displaying them for a reason I can't seem to pinpoint.
Model:
class Book(models.Model):
bookTitle = models.CharField(max_length = 100)
bookURL = models.SlugField()
bookContent = models.TextField()
bookAuthor = models.ForeignKey(User, on_delete = models.CASCADE, related_name = 'books')
creationDate = models.DateTimeField()
Template:
{% extends 'base.html' %}
{% block content %}
{% for p in books %}
<div class="post-wrapper col-lg-8 card" style="border: 1px solid black; margin: 20 auto;">
<div class="card-body">
<div class="card-title">{{ p.bookTitle }}</div>
<div class="card-body">
{{ p.bookContent }}
</div>
<em style="margin-left: 10px;"><small>written by {{ p.bookAuthor }} at {{ p.creationDate }}</small></em>
</div>
</div>
{% endfor %}
{% endblock %}
View:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Book
from django.utils import timezone
# Create your views here.
def viewAll(request):
books = Book.objects.all()
return render(request, 'viewAll.html', {})
Upvotes: 0
Views: 35
Reputation: 31494
You are not passing any data to the view context - you need to pass the books
in the context dictionary (third argument):
def viewAll(request):
books = Book.objects.all()
return render(request, 'viewAll.html', {'books': books}) # <--- this is currently empty
Upvotes: 3