1π
β
At first, you can simply apply @method_decorator
just above the class itself using name="dispatch'
.
Secondly, try to use many=True
in BookSerializer
and also use Book.objects.all()
to retrieve all instances of Book
model, like the following:
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
@method_decorator(login_required(login_url='login/'), name='dispatch')
class AddBookAPIView(APIView):
renderer_classes = [TemplateHTMLRenderer]
template_name = 'book.html'
def post(self, request):
book_serializer = BookSerializer(data=request.data)
if book_serializer.is_valid():
book_serializer.save()
return redirect('home')
return Response({'message': book_serializer.errors})
def get(self, request):
book = Book.objects.all()
serializer = BookSerializer(book, many=True)
return Response({'serializer': serializer, 'books': serializer.data})
Try to modify the template so that it would show the list of all books, like the following:
{% load rest_framework %}
<title>Books</title>
{% block title %}
<h3>View Books</h3>
{% endblock %}
{% block content %}
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Publication Date</th>
<th>Publisher</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{% for book in books %}
<tr>
<td>{{ book.name }}</td>
<td>{{ book.publication_date }}</td>
<td>{{ book.publisher }}</td>
<td>{{ book.description }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
π€Sunderam Dubey
Source:stackexchange.com