Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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,请帮助修复代码 我有一张表格: <form class="left filter" action="/userprofile/friends_search/" method="GET"> <div class="cell"> <label class="label lbl_name" for="fld_name">Имя</label> <input class="fld_name" type="te

请帮助修复代码

我有一张表格:

<form class="left filter" action="/userprofile/friends_search/" method="GET">
    <div class="cell">
        <label class="label lbl_name" for="fld_name">Имя</label>

        <input class="fld_name" type="text" name="fld_name" id="fld_name" value="">
    </div>          

    <div class="cell">
        <label class="label lbl_birth_date" for="fld_birth_date">Дата рождения</label>

        <input class="fld_birth_date datepicker hasDatepicker" type="text" name="fld_birth_date" id="fld_birth_date" value="">
    </div>                  

    <div class="cell">
        <input class="submit btn btn-default btn-block" type="submit" name="fld_submit" id="fld_submit" value="Найти">
    </div>
</form>
问题是控制器无法将参数搜索结果检查为空值:

@login_required 
def friends_search(request):
    search_result = None

    if request.method == 'GET' and not request.GET.get('fld_name'):
        search_result = 'empty'
    else:
        search_result = 'full'

        with open(os.path.join(settings.BASE_DIR, "search_result.txt"), "wb") as f:
            f.write(bytes(search_result, 'UTF-8'))      

    t = loader.get_template('friends_search.html')
    c = RequestContext(request, {
        'search_result': search_result,
    }, [custom_proc])   
    return HttpResponse(t.render(c))

导致调试文件search_result.txt写为“full”。但是“空”应该首先单独打印这两个条件,例如

print request.method, request.method == 'GET', not request.GET.get('fld_name')
看看哪个失败了

如果request.method不是GET则调试为什么不,如果是GET则尝试以下操作:

request.method.lower() == 'get'
如果第二个失败(不应该失败),请尝试以下操作:

request.method.lower() == 'get'

request.GET.GET('fld_name')!='
#但这会导致没有未处理的情况

是否发送post请求?请尝试
打印请求。方法
if语句前的行,以确保
请求。方法
实际上是
GET
。我猜
if语句在
request.method=='GET'
点失败。
@login_required 
def friends_search(request):
    search_result = None

    if request.method == 'POST':    
        if request.POST.get('fld_name'):
            try:
                fld_name = request.POST.get('fld_name')
                search_result = UserProfile.objects.filter(Q(nickname__icontains=fld_name))     
                if not search_result:
                    search_result = 'По вашему запросу ничего не найдено.'
            except Exception as exc:
                search_result = 'В данный момент доступ к базе данных невозможен. Попробуйте повторить ваш запрос позже.'   

    if isinstance(search_result, str):
        search_result_type = 'str'
    elif isinstance(search_result, dict):
        search_result_type = 'dict'
    else:
        search_result_type ='none'

    t = loader.get_template('friends_search.html')
    c = RequestContext(request, {
        'CHOICES_gender': UserProfile.get_CHOICES_gender(),
        'CHOICES_status': UserProfile.get_CHOICES_status(),
        'city_list': UserProfile.get_city_list(),
        'search_result': search_result,
        'search_result_type': search_result_type,
    }, [custom_proc])   
    return HttpResponse(t.render(c))