python - 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
解决方案
推荐阅读
- node.js - WebRTC Video Track 到 Node 中的 ffmpeg
- reactjs - 使用 Redux 基于按钮单击添加/删除数组中的对象
- ffmpeg - 无效的流说明符:元数据
- python - Tkinter:为什么下一个“标签”的 0 列会有所不同?
- ios - UICollectionViewCompositionalLayout - 具有fractionalWidth 宽度和自定尺寸单元格的意外布局
- python - github get_organization 给出 Not Found Error github.GithubException.UnknownObjectException: 404
- visual-studio-code - 使用 haxe 在 Visual Studio Code 中运行和调试问题
- python - 如何在@tf.function 修饰函数中使用张量作为切片索引?
- python - 我可以找出我的注册表中的错误在哪里吗?
- git - 如何从多个 git 组织中提取存储库