Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django for loop中的引导式基础导航_Python_Django - Fatal编程技术网

Python django for loop中的引导式基础导航

Python django for loop中的引导式基础导航,python,django,Python,Django,我试图在django中使用for循环使用bootstrap的基本导航来显示基于图书id的图书摘要和概要文件。我从“book.objects.all()”中得到“无法解析其余部分:”()”错误。请让我知道是否有任何其他方法来解决这个问题。谁能告诉我怎么做吗 book_review.html:- {% extends 'books/base.html' %} {% block content %} <div class = "container"> <ul

我试图在django中使用for循环使用bootstrap的基本导航来显示基于图书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 %}
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
URL.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)
urlpatterns=[
路径('admin/',admin.site.url),
路径(“”,views.home,name=“home”),
路径('books/',views.book\u review,name=“book\u review”),
路径('about/',views.about,name=“about”),
]+静态(settings.MEDIA\u URL,document\u root=settings.MEDIA\u 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>
然后在模板中迭代book对象,如下所示:

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 %}
{%forb在allBooks%}
{{b.作者}
{%endfor%}
{% for b in allBooks %}
   {{b.author}}
   <!-- and so on for other attributes -->
{% endfor %}