简体   繁体   English

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

[英]dynamically generate category list into base template with django class base views

views.py 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

Here BookList view is my home page. 这里的BookList视图是我的主页。 In my base template, I've added some kind of category such as SingleCategoryView, SingleLanguage view etc as dynamic URL dropdown in the navbar. 在我的基本模板中,我在导航栏中添加了一些类别,例如SingleCategoryView,SingleLanguage视图等作为动态URL下拉列表。

base.html 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 -->

whilst on the homepage or any other category list page that categories dropdown navbar are showing good but when I'm going to in my BookDetail page it doesn't show. 虽然在首页或任何其他类别列表页面上,类别下拉列表导航栏显示良好,但是当我要进入BookDetail页面时,它不会显示。 I commented out that codes into BookDetail View. 我将这些代码注释到BookDetail视图中。

Please visit this link, you will understand clearly. 请访问此链接,您将清楚地了解。 https://clean-book-library.herokuapp.com/ Keep your cursor author, language or category. https://clean-book-library.herokuapp.com/保持您的光标作者,语言或类别。 You will see the list and then go to the details page of any book and keep your cursor again you won't see that. 您将看到列表,然后转到任何书籍的详细信息页面,再次保持光标位置,您将不会看到它。 The main question, why doesn't work BookDetails View code that I've commented out and how to create base search bar into one class views such like as dropdown navbar. 主要问题是,为什么我已注释掉的BookDetails View代码不起作用,以及如何将基本搜索栏创建为一个类视图(如下拉导航栏)。

Thanks. 谢谢。

You can pass extra context data in get_context_data function. 您可以在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM