python - 我的 render_to_response 字典中的某些值为空

标签 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
..    
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 语句中使用它们。我什至在返回之前设置了 variables['show_request_list']= True(作为测试),但它仍然显示为 null。

这是我的模板的一部分:

{% 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 %}

关于如何调试它的任何想法?如果您需要更多信息,请告诉我。

更新:我的模板中的 if 语句已经整理完毕。然而,一些字典值仍然是空的。这是一个关键:

上面的变量 ['show_request_list'] 根据查询字符串值设置为 true 或 false:

if request.GET.get('show_requests', ''):
        show_requests = int(request.GET.get('show_requests'))
if show_requests==1:
        variables['show_request_list']= True

默认情况下,变量 ['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

variables['servicerequests'] 应该至少有一个记录,因为我的数据库中有值。当我尝试同时呈现 {{ servicerequests }} 和 {{ show_request_list }} 时,没有任何显示。

提前致谢。

最佳答案

问题不是你想的那样。只是 TrueFalse 不会自动出现在模板上下文中,因此(与任何其他不存在的模板变量一样)它们默认为 None,并且您的比较失败。

然而,无论如何都没有理由明确地与这些值进行比较。与 Python 代码一样,您应该简单地进行 bool 测试:

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

关于python - 我的 render_to_response 字典中的某些值为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28434571/

相关文章:

django - “讨厌的 “Table ' my_table'已经存在”在Django-South中

django - 管理中缺少字段

python - 更改 is_superuser Django 的 verbose_name

python - Emacs Python 自动补全

python - 如何选择范围内的随机数 - + 转换为 ms

python - 'as' 运算符是做什么的?

python - 为什么不总是使用 Python 属性?

python - 非常基本的带参数的函数

python - 如何划分两个DataFrame

python - Django ImageField问题