0

I am working on library website in which I want to display data from database which is Book name and description. But I'm not able to do that. Here is my code

views.py

from django.shortcuts import render
from .models import *
def index(request):
   book_list = Book.objects.all()
   return render(request,template_name='index.html', context={'book_list':book_list})

index.html

{% extends "base_generic.html" %}
{% block new_books %}
{% for b in book_list %}
    <div class="card">
        <img class="card-img-top" src=".." alt="Image">
        <div class="card-body">
            <h5 class="card-title">{{ book_list }} </h5>
            <p class="card-text">Hello this is card text</p>
            <a class="btn btn-primary">View this book</a>
        </div>
    </div>
{% endfor %}
{% endblock %}

1 Answer 1

4

You should work with b variable instead of book_list inside of for loop.

If your Book model contains title field, your code might look like this:

{% extends "base_generic.html" %}
{% block new_books %}
{% for b in book_list %}
    <div class="card">
        <img class="card-img-top" src=".." alt="Image">
        <div class="card-body">
            <h5 class="card-title">{{ b.title }} </h5>
            <p class="card-text">Hello this is card text</p>
            <a class="btn btn-primary">View this book</a>
        </div>
    </div>
{% endfor %}
{% endblock %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.