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

时间:2016-01-31 23:15:17

标签: django

我一直在关注一个教程并且已经调整了一下。我在所有网站页面上都有一个搜索字段,即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)

继承人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)

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

1 个答案:

答案 0 :(得分:0)

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

在您的情况下,使用docs

中的ListView和DetailView
from django.views.generic import ListView , DetailView

from .models import Foo

class FooListView(ListView):
    model = Foo

class FooDetailview(DetailView):
    model = Foo