使用django类基本视图动态生成类别列表到基本模板中

时间:2019-01-27 11:16:32

标签: python html django dynamic

views.py

from django.shortcuts import render, get_object_or_404
from django.utils.decorators import method_decorator
from django.views.decorators.gzip import gzip_page
from django.views.decorators.http import condition
from django.views.generic.detail import SingleObjectMixin
from django.utils import timezone
from django.views.generic import \
    ListView, DetailView
from .models import (
    Book,
    Category,
    Author,
    Language,
    Currency,
    Tag,
)
from django.db.models import Q

class BookList(ListView):
    model = Book
    context_object_name = 'book_list'
    template_name = 'books/book_lists.html'
    paginate_by = 12
    extra_context = {
        'category_list': Category.objects.all(),
        'author_list': Author.objects.all(),
        'language_list': Language.objects.all(),
    }

    def get_queryset(self):
        query = self.request.GET.get('q')
        if query:
            object_list = self.model.objects.filter(
                Q(name_of_the_book__icontains=query) |
                Q(author__first_name__icontains=query) |
                Q(category__name__icontains=query)
            )
        else:
            object_list = self.model.objects.all()
        return object_list


class SingleCategoryView(DetailView):
    model = Category
    template_name = 'books/single_category.html'
    paginate_by = 12
    extra_context = {
        'category_list': Category.objects.all(),
        'author_list': Author.objects.all(),
        'language_list': Language.objects.all(),
    }


class SingleAuthorView(DetailView):
    model = Author
    template_name = 'books/single_author.html'
    extra_context = {
        'category_list': Category.objects.all(),
        'author_list': Author.objects.all(),
        'language_list': Language.objects.all(),
    }


class SingleLanguage(DetailView):
    model = Language
    template_name = 'books/single_language_list.html'
    extra_context = {
        'category_list': Category.objects.all(),
        'author_list': Author.objects.all(),
        'language_list': Language.objects.all(),
    }


class BookDetails(DetailView):
    model = Book
    template_name = 'books/book_details.html'
    # extra_context = {
    #     'category_list': Category.objects.all(),
    #     'author_list': Author.objects.all(),
    #     'language_list': Language.objects.all(),
    # }

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['now'] = timezone.now()
        return context

“ BookList”视图是我的主页。在我的基本模板中,我在导航栏中添加了一些类别,例如SingleCategoryView,SingleLanguage视图等作为动态URL下拉菜单。

base.html

<!-- nav bar -->
<div class="navbar">

  <a href="{% url 'library:book_list' %}">Home</a>
  <div class="dropdown">
    <button class="dropbtn">Category
      <i class="fa fa-caret-down"></i>
    </button>

    <div class="dropdown-content">
        {% for object in category_list %}
       <a href="{% url 'library:single_category_details' object.slug %}">{{ object.name }}</a>
        {% endfor %}
    </div>
  </div>

  <div class="dropdown">
    <button class="dropbtn">Author
      <i class="fa fa-caret-down"></i>
    </button>

    <div class="dropdown-content">
        {% for object in author_list %}
       <a href="{% url 'library:single_author_details' object.slug %}">{{ object.first_name }} {{ object.last_name }}</a>
        {% endfor %}
    </div>
  </div>

  <div class="dropdown">
    <button class="dropbtn">Language
      <i class="fa fa-caret-down"></i>
    </button>

    <div class="dropdown-content">
        {% for object in language_list %}
       <a href="{% url 'library:single_language_list' object.slug %}">{{ object.language }}</a>
        {% endfor %}
    </div>
  </div>

</div>
<!-- nav bar end -->

在首页或任何其他类别列表页面上,类别下拉导航栏显示都很好,但是当我要进入BookDetail页面时,它不会显示。我将这些代码注释到BookDetail视图中。

请访问此链接,您将清楚地了解。 https://clean-book-library.herokuapp.com/ 保持您的光标作者,语言或类别。您将看到列表,然后转到任何书籍的详细信息页面,再次保持光标位置,您将不会看到它。 主要问题是,为什么我已注释掉的BookDetails View代码不起作用,以及如何将基本搜索栏创建为一个类视图(如下拉导航栏)。

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以在get_context_data函数中传递额外的上下文数据。

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['now'] = timezone.now()
    context.update(self.extra_context)
    return context