如何向django的所有模板添加搜索栏功能?

如何向django的所有模板添加搜索栏功能?,django,Django,我正试图在我的django站点中添加一个搜索栏。我在基本模板中添加了一个表单。我已将该功能添加到我的主视图中。它在主视图中工作。但在其他观点中并非如此。我尝试创建一个新视图,但I.e.不起作用。请告诉我该怎么办 views.py: def搜索(请求): 如果request.method==“GET”: query=request.GET.GET('q').split() 如果查询: queryset=reduce( 接线员, [ Q(title_uicontains=查询)| Q(content

我正试图在我的django站点中添加一个搜索栏。我在基本模板中添加了一个表单。我已将该功能添加到我的主视图中。它在主视图中工作。但在其他观点中并非如此。我尝试创建一个新视图,但I.e.不起作用。请告诉我该怎么办

views.py:

def搜索(请求):
如果request.method==“GET”:
query=request.GET.GET('q').split()
如果查询:
queryset=reduce(
接线员,
[
Q(title_uicontains=查询)| Q(content_uicontains=查询)
用于查询中的查询
]
)
posts=Post.objects.filter(queryset.distinct())
返回呈现(请求“blog/home.html”,{'posts':posts})
模板:-

<form class="form-inline my-2 my-lg-0">
                            <input class="form-control mr-sm-2" action="{% url 'blog-home' %}" method="GET" type="search" placeholder="Search Posts" aria-label="Search" name="q">
                            <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
                        </form>

搜寻
url.py

urlpatterns = [
    path('', PostListView.as_view(), name='blog-home'),
    path('user/<str:username>', UserPostListView.as_view(), name='user-posts'),
    path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
    path('post/new/', PostCreateView.as_view(), name='post-create'),
    path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
    path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
    path('about/', views.about, name='blog-about'),
    path('comment/<int:pk>/approve/', views.comment_approve, name='comment_approve'),
    path('comment/<int:pk>/remove/', views.comment_remove, name='comment_remove'),
    path('comment/<int:pk>/delete/', views.comment_delete, name='comment_delete'),
    path('summernote/', include('django_summernote.urls')),
    path('feedback/', views.contact_us, name='contact_us'),
    path('search/', views.search),
]
urlpatterns=[
路径(“”,PostListView.as_view(),name='blog-home'),
路径('user/',UserPostListView.as_view(),name='user-posts'),
路径('post/',PostDetailView.as_view(),name='post-detail'),
路径('post/new/',PostCreateView.as_view(),name='post-create'),
路径('post//update/',PostUpdateView.as_view(),name='post-update'),
路径('post//delete/',PostDeleteView.as_view(),name='post-delete'),
路径('about/',views.about,name='blog-about'),
路径('comment//approve/',views.comment\u approve,name='comment\u approve'),
路径('comment//remove/',views.comment\u remove,name='comment\u remove'),
路径('comment//delete/',views.comment\u delete,name='comment\u delete'),
路径('summernote/',包括('django_summernote.url'),
路径('feedback/',views.contact_-us,name='contact_-us'),
路径('search/',views.search),
]

在模板表单标签中添加要加载搜索功能的URL名称。通过这种方式,您可以从任何页面进行搜索,并将重定向到该页面。如果你不明白,我可以给你一个参考代码,这样你就可以掌握一些想法

<form method="GET" action="{% url 'courses' %}">

这就是我执行搜索功能的方式。首先我们得到我们想要的模型的对象,然后我们得到q,它是我的模板中输入的名称,然后根据我们想要的字段进行过滤。我前面提到的表单操作是解决您问题的方法

view.py

def course_view(request):
latest_course = Course.objects.all()
query = request.GET.get('q')
print(query)
if query:
    latest_course = Course.objects.filter(
        Q(title__icontains=query)|
        Q(user__full_name=query)|
        Q(price__icontains=query)
    )
context = {
    'latest_course': latest_course,
}

return render(request, 'apps/course.html', context)
html(它在我的基本模板中,就像你的模板一样)



请提供一些代码和更多详细信息。您需要提供一些代码,以便人们可以帮助您。请提供参考代码。因为我尝试了很多方法,但都没有成功。你能更详细、更具体地说明错误吗?运行网站时没有错误。但它不起作用。我创建了一个新的视图(搜索),在该视图中,我试图通过导航栏获取与搜索栏传递的查询相关的帖子,导航栏位于我的基本模板中。如果您看到上面的代码,我正在使用用户传递的查询的搜索结果呈现我的主模板。现在,当我在搜索栏中输入query时,任何事情都在发生。
      <div class="sidebar-search">
            <form method="GET" action="{% url 'courses' %}">
              <div class="input-group">
                <input type="text" class="form-control search-menu" placeholder="Search..." name='q'>
                <div class="input-group-append">
                  <span class="input-group-text">
                    <i class="fa fa-search" aria-hidden="true"></i>
                  </span>
                </div>
              </div>
            </form>
          </div>