在django中为循环引导基本导航



我正在尝试使用django中bootstrap的基本导航,使用for循环来显示基于图书id的图书摘要和配置文件;无法分析"book.objects.all(("中的余数:"((";错误如果有其他方法可以解决这个问题,请告诉我。有人能告诉我怎么走吗?

book_review.html:-

{% extends 'books/base.html' %}
{% block content %}
<div class = "container">
<ul class = "nav nav-tabs" id = "myTab" role = "tablist">
<li class = "nav-item">
<a class = "nav-link active" id = "summary-tab" data-toggle = "tab" 
href = "#summary" role = "tab" aria-controls = "summary" 
aria-selected = "true">Summary</a>
</li>
<li class = "nav-item">
<a class = "nav-link" id = "profile-tab" data-toggle = "tab" 
href = "#profile" role = "tab" aria-controls = "profile" 
aria-selected = "false">Profile</a>
</li>
<li class = "nav-item">
<a class = "nav-link" id = "relatedbooks-tab" data-toggle = "tab" 
href = "#relatedbooks" role = "tab" aria-controls = "relatedbooks" 
aria-selected = "false">Related Books</a>
</li>
</ul>
<div class = "tab-content" id = "myTabContent">
<div class = "tab-pane fade show active" id = "summary" role = "tabpanel" 
aria-labelledby = "summary-tab"><br><br>
{% for booksummary in book.objects.all() %}
{{booksummary.summary}}
{% endfor %}
</div>

<div class = "tab-pane fade" id = "profile" role = "tabpanel" 
aria-labelledby = "profile-tab">
{% for bookprofile in book.objects.all() %}
<b><label for="title">Title:</label></b>
<p class="card-title">{{bookprofile.title}}</p>
<b><label for="author">Author:</label></b>
<p class="card-text">{{bookprofile.author}}</p>
<b><label for="release_date">Release Date:</label></b>
<p class="card-text">{{bookprofile.release_date}}</p>
<b><label for="language">Language:</label></b>
<p class="card-text">{{bookprofile.language}}</p>
<b><label for="genre">Genre:</label></b>
<p class="card-text">{{bookprofile.genre}}</p><br>
{% endfor %}
</div>

<div class = "tab-pane fade" id = "relatedbooks" role = "tabpanel" 
aria-labelledby = "relatedbooks-tab">Content for related books tab</div>
</div>
<br>
<!-- jQuery library -->
<script src = "https://code.jquery.com/jquery-3.2.1.slim.min.js" 
integrity = "sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" 
crossorigin = "anonymous">
</script>
<!-- Popper -->
<script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" 
integrity = "sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" 
crossorigin = "anonymous">
</script>
<!-- Latest compiled and minified Bootstrap JavaScript -->
<script src = "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" 
integrity = "sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" 
crossorigin="anonymous">
</script>

{% endblock %}

views.py:

def book_review(request,id):
book = get_object_or_404(Bookslist, id=id)
return render(request, "books/book_review.html", {'book': book})

models.py:

class Bookslist(models.Model):
title=models.CharField(max_length=30, unique=True)
author=models.CharField(max_length=30)
release_date= models.DateField()
language= models.CharField(max_length=30)
genre= models.CharField(max_length=30)
image= models.ImageField(upload_to='book_images/')
summary = models.TextField(blank=True, null=True)
def __str__(self):
return self.title

urls.py:

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name="Home"),
path('books/<int:id>/', views.book_review, name="book_review"),
path('about/', views.about, name="about"),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

您不会在模板中传递这样的查询集。如果你只想显示一本书的详细信息,那么它应该是

<div class = "tab-pane fade show active" id = "summary" role = "tabpanel" aria-labelledby = "summary-tab">
<br><br>
{{book.summary}}
</div>
<div class = "tab-pane fade" id="profile" role = "tabpanel" aria-labelledby = "profile-tab">
<b><label for="title">Title:</label></b>
<p class="card-title">{{book.title}}</p>
<b><label for="author">Author:</label></b>
<p class="card-text">{{book.author}}</p>
<b><label for="release_date">Release Date:</label></b>
<p class="card-text">{{book.release_date}}</p>
<b><label for="language">Language:</label></b>
<p class="card-text">{{book.language}}</p>
<b><label for="genre">Genre:</label></b>
<p class="card-text">{{book.genre}}</p>
<br>
</div>

如果你想显示所有的书,那么你的视图将是这样的:

def book_review(request,id):
context ={}
book = get_object_or_404(Bookslist, id=id)
context['book'] = book
context['allBooks'] = Bookslist.objects.all()
return render(request, "books/book_review.html", context)

然后你将在模板中迭代你的书对象,如下所示:

{% for b in allBooks %}
{{b.author}}
<!-- and so on for other attributes -->
{% endfor %}

最新更新