django for loop中的引导基础导航
Posted
技术标签:
【中文标题】django for loop中的引导基础导航【英文标题】:Bootstrap base navs in django for loop 【发布时间】:2021-02-02 23:56:12 【问题描述】:我正在尝试在 django 中使用引导程序的基本导航,使用 for 循环根据书本 ID 显示书本的摘要和配置文件。我收到“无法解析余数:'()' from '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)
【问题讨论】:
【参考方案1】:您不会在模板中传递这样的查询集。如果您只想显示一本书的详细信息,那么它应该是
<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 %
【讨论】:
以上是关于django for loop中的引导基础导航的主要内容,如果未能解决你的问题,请参考以下文章