我的搜索字段在我的post_detail页面上不起作用

时间:2021-05-19 08:06:34

I've been following an tutorial and have been tweaking it a bit. I have a search field on all of my sites pages, which are post_list, post_detail, and tags_list. Both post_list and tags_list have paginators and the code for each looks like the following:

我一直在关注一个教程并且已经调整了一下。我在所有网站页面上都有一个搜索字段,即post_list,post_detail和tags_list。 post_list和tags_list都有分页器,每个分区的代码如下所示:

tags_list:

def tag_list(request, slug=None):
   instance = get_object_or_404(Tag, slug=slug)
   ins = instance.post_set.all()
   queryset_list = Post.objects.active()
   if request.user.is_staff or request.user.is_superuser:
       queryset_list = Post.objects.all()

   query = request.GET.get("q")
   if query:
       queryset_list = queryset_list.filter(
               Q(title__icontains=query) |
               Q(content__icontains=query) |
               Q(user__first_name__icontains=query) |
               Q(user__last_name__icontains=query)
       ).distinct()
   paginator = Paginator(queryset_list, 1)
   page_request_var = "tags"
   page = request.GET.get(page_request_var)
   try:
       queryset = paginator.page(page)
   except PageNotAnInteger:
       queryset = paginator.page(1)
   except EmptyPage:
       queryset = paginator.page(paginator.num_pages)
   hey = paginator.num_pages
   context = {
       "queryset": queryset,
       "paginator": paginator,
       "page_request_var": page_request_var,
       "hey": hey,
       "title": "posts"
   }
   return render(request, "posts/tag_list.html", context)

post_list:

def post_list(request):
   today = timezone.now().date()
   queryset_list = Post.objects.active()
   if request.user.is_staff or request.user.is_superuser:
       queryset_list = Post.objects.all()
   query = request.GET.get("q")
   if query:
       queryset_list = queryset_list.filter(
               Q(title__icontains=query) |
               Q(content__icontains=query) |
               Q(user__first_name__icontains=query) |
               Q(user__last_name__icontains=query)
       ).distinct()
   paginator = Paginator(queryset_list, 6)
   page_request_var = "page"
   page = request.GET.get(page_request_var)
   try:
       queryset = paginator.page(page)
   except PageNotAnInteger:
       queryset = paginator.page(1)
   except EmptyPage:
       queryset = paginator.page(paginator.num_pages)
   hey = paginator.num_pages
   context = {
           "queryset": queryset,
           "title": "posts",
           "page_request_var": page_request_var,
           "today": today,
           "paginator": paginator,
           "hey": hey
       }

return render(request, "posts/post_list.html", context)

heres the post_detail

继承了post_detail

def post_detail(request, slug=None):
   instance = get_object_or_404(Post, slug=slug)


   queryset_list = Post.objects.active()
   if request.user.is_staff or request.user.is_superuser:
       queryset_list = Post.objects.all()
   query = request.GET.get("q")
   if query:
       queryset_list = queryset_list.filter(
               Q(title__icontains=query) |
               Q(content__icontains=query) |
               Q(user__first_name__icontains=query) |
               Q(user__last_name__icontains=query)
       ).distinct()


   if instance.publish > timezone.now().date() or instance.draft:
       if not request.user.is_staff or not request.user.is_superuser:
           raise Http404
   share_string = quote_plus(instance.content)
   context = {
       "title": "detail ",
       "instance": instance,
       "share_string": share_string,
       "query": query
   }
   return render(request, "posts/post_detail.html", context)

I'm not quite sure how to arrange the syntax in the post_detail, due to the fact it doesn't use a paginator. Also I am breaking the DRY rule of Django. How could I implement my search and adhere to the DRY principle? Any help would be great.

我不太确定如何在post_detail中安排语法,因为它不使用paginator。我也打破了Django的DRY规则。我怎样才能实现我的搜索并坚持DRY原则?任何帮助都会很棒。

1 个解决方案

#1


0  

You should use the Class-bassed views, the CBV's help you to write clean and maintainable code!

您应该使用基于类的视图,CBV可以帮助您编写干净且可维护的代码!

In your case, use ListView and DetailView, from the docs:

在您的情况下,使用文档中的ListView和DetailView:

from django.views.generic import ListView , DetailView

from .models import Foo

class FooListView(ListView):
    model = Foo

class FooDetailview(DetailView):
    model = Foo

#1


0  

You should use the Class-bassed views, the CBV's help you to write clean and maintainable code!

您应该使用基于类的视图,CBV可以帮助您编写干净且可维护的代码!

In your case, use ListView and DetailView, from the docs:

在您的情况下,使用文档中的ListView和DetailView:

from django.views.generic import ListView , DetailView

from .models import Foo

class FooListView(ListView):
    model = Foo

class FooDetailview(DetailView):
    model = Foo