Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的搜索哪里出错了?_Python_Django - Fatal编程技术网

Python 我的搜索哪里出错了?

Python 我的搜索哪里出错了?,python,django,Python,Django,views.py def userlogout(request): logout(request) return HttpResponseRedirect(reverse('userlogin')) def Search(request): if request.method == 'POST': search=request.GET['srch'] if search: match=Blog.object

views.py

def userlogout(request):
    logout(request)
    return HttpResponseRedirect(reverse('userlogin'))

def Search(request):
    if request.method == 'POST':
        search=request.GET['srch']
        if search:
                match=Blog.objects.filter(Q( blog_title_icontains=search)|
                                      Q( blog_description_icontains=search)|
                                      Q(blogcategories_icontains=search) )
                if match:
                    return render (request,"search.html",{"sr":match})
                else:
                      messages.error(request,"no results found")
        else:
         return HttpResponseRedirect('/search/')

    return render (request,'index.html')
#

index.html

<form action="{%url 'search' %}" method="post" class="form-inline my-2 my-lg-0 header-search">
                        {% csrf_token %}
                    <input class="form-control mr-sm-2" type="search" placeholder="Search here..." name="Search" required="">
                    <button class="btn btn1 my-2 my-sm-0" type="submit">
                        <i class="fas fa-search"></i>
                    </button>
                </form>
*****这给了我一个错误:

Exception Type: MultiValueDictKeyError
Exception Value:    
'srch'

请帮助我如何使用现有模板在我的博客中搜索。

您有几个错误

您的搜索字段称为
search
,而不是
srch
。我不知道你从哪里得到的
srch
,因为你从来没有在模板中使用过它

您的表单正在通过POST提交,但您正在尝试从get获取数据

但事实上,搜索表单应该由GET而不是POST提交,因为它不会在后端进行更改,而且可以被缓存

因此,您需要:

<form action="{%url 'search' %}" method="get" class="form-inline my-2 my-lg-0 header-search">

我不擅长python,但看起来像是
request.GET['srch']
给你一个错误,因为你的
输入
字段有不同的
名称
<代码>我按照你说的做了尝试,但没有返回任何内容。你能帮助我如何使用模板中的内置搜索表单在我的博客中添加搜索功能吗?我完全按照你说的做了,但没有成功
<form action="{%url 'search' %}" method="get" class="form-inline my-2 my-lg-0 header-search">
def Search(request):
    search=request.GET['Search']
    if search: