Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.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 在我的render\u to\u响应字典中,某些值为空_Python_Django - Fatal编程技术网

Python 在我的render\u to\u响应字典中,某些值为空

Python 在我的render\u to\u响应字典中,某些值为空,python,django,Python,Django,我已经为此奋斗了几天,现在我被难倒了。在views.py函数的顶部,我设置了一个空字典: variables = {} 通过我的函数,我将值添加到字典中 line: 710: variables['isolate_location'] = True ... line 715: variables['show_request_list']= False .. line 748: variables['servicerecords'] = service_records_list .. l

我已经为此奋斗了几天,现在我被难倒了。在views.py函数的顶部,我设置了一个空字典:

variables = {}
通过我的函数,我将值添加到字典中

line: 710: variables['isolate_location'] = True
...
line 715: variables['show_request_list']= False
..
line 748: variables['servicerecords'] = service_records_list
..    
line 809: variables['form'] = service_record_form
..
..
..
    return render_to_response('locations/location_servicerecords.html', variables,
                          context_instance=RequestContext(request))
不,当我的模板加载上面列出的4个值中的3个时,结果为空(第710、715和809行)。我只是在模板的if语句中使用它们。我甚至在返回之前(作为测试)将变量['show_request_list']=True设置为空

以下是我的模板的一部分:

{% if user.is_staff %} {# The add button, ensures add button visible only when user has permission #}
    <a href="{% url 'admin:locations_servicerecord_changelist' %}?q={{ location.id }}" class="btn btn-default btn-xs"><i class="fa fa-edit"></i> Admin</a>
    <a href="{% url 'location_servicerecords' location.id %}?show_records=1&show_requests=0" class="btn btn-default btn-xs"><i class="fa fa-edit"></i> Service Records</a>
    {% if isolate_location == True %}
        <a href="{% url 'location_servicerecords' location.id %}?show_records=0&show_requests=1" class="btn btn-default btn-xs"><i class="fa fa-edit"></i> Service Requests</a>
    {% endif %}
    {% if show_request_list == False %}
        <a href="#addServiceRecord" role="button" class="btn btn-info btn-xs" data-toggle="modal" data-target=""><i class="fa fa-plus"></i> Add Service Record</a>
    {% elif show_request_list == True %}
        <a href="#addServiceRequest" role="button" class="btn btn-info btn-xs" data-toggle="modal" data-target=""><i class="fa fa-plus"></i> Add Service Request</a>
    {% endif %}
{% endif %}
默认情况下,变量['show_request_list']设置为false

我使用该变量模板来显示变量['servicerequests'],其构建如下所示:

 cur_loc_active_service_reqs = ServiceRequest.objects.filter(location=location_id).order_by(
        '-request_made_date')

    #Pagination
    service_requests_paginator = Paginator(cur_loc_active_service_reqs, RECORDS_PER_PAGE)  # Show 25 contacts per page

    try:
        service_request_list = service_requests_paginator.page(service_request_page)
    except PageNotAnInteger:
        if service_request_id:
            service_request_page = cur_loc_active_service_reqs.filter(service_date__gt=cur_service_rec.service_date).count()/RECORDS_PER_PAGE+1
        else:
            service_request_page = 1
        service_request_list = service_requests_paginator.page(service_request_page)
    except EmptyPage:  # If page is out of range (e.g. 9999), deliver last page of results.
        service_request_list = service_requests_paginator.page(service_requests_paginator.num_pages)


    if service_request_list:
        variables['servicerequests'] = service_request_list
变量['servicerequests']应至少有一条记录,因为my DB中有值。当我尝试呈现两个{{servicerequests}}&{{show_request_list}时,没有显示任何内容


提前谢谢

问题不在于你认为它是什么。很简单,
True
False
不会自动出现在模板上下文中,因此(与任何其他不存在的模板变量一样),它们默认为无,并且比较失败

但是,无论如何,没有理由显式地与这些值进行比较。与Python代码一样,您只需执行布尔测试:

{% if isolate_location %}
    ...
{% endif %}
{% if not show_request_list %}
    ...
{% elif show_request_list %}
    ...
{% endif %}

您说过您在
if
语句中使用这些,如果您只是尝试呈现值,例如
{{isolate_location}}
{form}
,会发生什么?您能显示您的模板代码吗?#我们需要查看您的代码以及初始声明的具体位置?你的意思是它在文件的最顶端,在任何实际的视图函数之外吗?@Joseph当我试图渲染这些值时,根本没有渲染任何内容。@DanielRoseman,抱歉(原始帖子已更新)。初始声明位于我的功能顶部。谢谢。这对我的支票确实有帮助。我的一些其他字典值,比如form,仍然被认为是无用的。我在函数中声明的东西正确吗?请首先说明表单来自何处。好的,我算出了我的表单。我将用另一个我正在苦苦挣扎的字典值来更新原始帖子。谢谢DanielAny的想法?我的代码有意义吗?或者我还可以提供其他东西吗?我开始出现以下错误。我的错误和我最初的问题有关系吗:TypeError:列表索引必须是整数,而不是str-它是针对我的变量抛出的={}我真的被难倒了
{% if isolate_location %}
    ...
{% endif %}
{% if not show_request_list %}
    ...
{% elif show_request_list %}
    ...
{% endif %}