首页 > 解决方案 > Django:不显示书名和图像

问题描述

该代码在引导主题之前可以正常工作,但现在不显示书名和图像。

base.html文件:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
     <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static '/books/style.css' %}" >
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
              integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

</head>
<body>
    <nav class="navbar navbar-inverse">
            <div class="container-fluid">
                <a class="navbar-brand" href="{% url 'books:index' %}">The Bookstore</a>
                <div class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li class="">
                        <a href="{% url 'books:index' %}">
                             <span class="glyphicon glyphicon-book" aria-hidden="true"></span>  Books
                        </a>
                    </li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li class="">
                        <a href="{% url 'books:book-add' %}">
                            <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>  Add Book
                        </a>
                    </li>
                    <li class="">
                        <a href="{% url 'books:index' %}">
                            <span class="glyphicon glyphicon-log-out" aria-hidden="true"></span>  Logout
                        </a>
                    </li>
                </ul>

            </div>
            </div>


       </nav>
        {% block body %}
        {% endblock %}

</body>
</html>

以下 HTML 文件应该显示书名和图像,但它显示{{book.name}}但不显示book_image. 数据库已创建并已存储,但未显示。

index.html文件:

    {% extends 'books/base.html' %}
    {% block body %}
    <link rel="stylesheet" href="books/style.css">
    <div class=" col-md-12 jumbotron">
      <h1>The Bookstore</h1>
      <p>Collection of all popular books</p>
    </div>
    <div class=" col-md-10 jumbotron jumbotron-special" name="fig">
       <div class="col-md-12 span-2">
      <h2>Popular Books</h2>

      </div>
    </div>

    <ul>
       {% for book in object_list %}

       <div class="col-lg-3 col-md-4 col-sm-6 " >
          <div class = "thumbnail">
             <img src = "{‌{book.book_image}}" alt = "Generic placeholder thumbnail">


             <div class = "caption">
              <h3>{‌{book.name}}</h3>
              <p>{‌{book.author}}</p>

              <p>
                 <a href = "{% url 'books:detail' book.id %}" class = "btn btn-primary" role = "button">
                    Details
                  </a>

                  <a href = "#" class = "btn btn-danger" role = "button">
                     Delete
                  </a>
             </p>
             </div>
          </div>
     </div>
       {% endfor %}
    </ul>
    {% endblock %}

这是定义函数的视图文件:

from django.views import generic
from .models import Book
from django.views.generic.edit import CreateView


class IndexView(generic.ListView):
    template_name = 'books/index.html'


    def get_queryset(self):
        return Book.objects.all()

class BookCreate(CreateView):
    model = Book
    fields = ['name', 'author', 'price', 'types', 'book_image']


class DetailView(generic.DetailView):
    model = Book
    template_name = 'books/detail.html

标签: pythonhtmldjango

解决方案


推荐阅读