2016-02-18 47 views
0

我目前在我的base.html中有一個搜索字段。繼承人的片段如何創建一個在我所有的Django視圖中都有效的搜索

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation"> 
     <div class="container"> 
     <div class="navbar-header"> 
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> 
      <span class="sr-only">Toggle navigation</span> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
      </button> 
      <a class="navbar-brand" href="{% url 'posts:list' %}">HEHTS</a> 
     </div> 
     <div id="navbar" class="collapse navbar-collapse"> 


      <form method="GET" action=" " class="navbar-form navbar-right"> 
      <div class="input-group">{% csrf_token %} 
       <input type="text" name="q" id="search" placeholder="search" value="{{ request.GET.q }}" class="form-control" 
       style="width: 350px"> 


      <!--<input type="text" name="q" placeholder="search post" value="{{ request.GET.q }}"/>--> 
      <!--<input type="submit" value="Search" />--> 
     <!--</form>--> 

       <ul id="search-results"> 

       </ul> 

      <span class="input-group-btn"> 
      <button type="submit" class="btn btn-primary"><span style="font-size:1.4em" class="glyphicon glyphicon-search"></span> </button> 
      </span> 
      </div> 
      </form> 

      <ul class="nav navbar-nav"> 
      <li><a href="{% url 'posts:list' %}">Home</a></li> 
      <!--<li><a href="#">Sewp</a></li>--> 
      {% if user.is_authenticated %} 
      <li><a href="{% url 'admin:index' %}">Admin</a></li> 
      <li><a href="{% url 'posts:create' %}">Create</a></li> 
      {% endif %} 

      </ul> 
     </div><!--/.nav-collapse --> 
     </div> 
</nav> 

這在我的detail.html視圖中不起作用。它只是在我post_list.html鑑於繼承人的作品對他們倆的意見

def post_detail(request, slug=None): 
     instance = get_object_or_404(Post, slug=slug) 
     if instance.publish > timezone.now().date() or instance.draft: 
      if not request.user.is_staff or not request.user.is_superuser: 
       raise Http404 

     queryset_list = Post.objects.active() 
     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() 


     share_string = quote_plus(instance.content) 
     template = "posts/post_detail.html" 
     context = { 
      "instance": instance, 
      "name": instance.title, 
      "share_string": share_string, 
      "queryset_list": queryset_list 
     } 
     return render(request, template, context) 


    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, 8) 
     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) 
     template = "posts/post_list.html" 
     name = "user" 
     context = { 
      "object_list": queryset, 
      "name": name, 
      "page_request_var": page_request_var, 
      "today": today, 
     } 
     return render(request, template, context) 

我需要的結果是讓事情變得乾燥,只需編寫一次代碼,並能夠在我的base.html文件使用它無論我渲染什麼視圖,它都可以工作。 我該如何解決這個問題,所以我的博客應用程序可以在任何視圖中工作?歡迎所有幫助或建議。

回答

1

我認爲這可能很簡單。只要給搜索專用的瀏覽功能,並且相應的網址,讓我們說:

url(r'^post-search/$', views.post_search, name='post-search') 

然後你把你當前的搜索形式base.html文件,並在窗體的行動指向post-search網址的變化:

<form method="GET" action="{% url 'post-search' %}" class="navbar-form navbar-right"> 
    <!-- same content goes here --> 
</form> 

因此,現在每次提交此表單時,都會執行查看功能post_search,並執行相同的過程並顯示結果。

Some explanations about form action attribute

+0

感謝您的回覆。我會嘗試。如果你是一名設計師,你會這麼做嗎?這是專業的方式嗎? – losee

+0

我不是設計師,我不會給人們建議,我不知道/使用。 –

相關問題