【问题标题】:Django response.session behaving differently on localhostDjango response.session 在本地主机上的行为不同
【发布时间】:2015-08-29 08:39:02
【问题描述】:

我在 Django (v1.8) 应用程序中有以下几行。

{% if failed < 5 %}
    <div class="centered row">
        <div class="ui error message six wide column">
            <div class="content">
                The username and password provided do not match. Please try again.
            </div>
        </div>
    </div>
{% endif %}

failed 像这样被传递给模板:

if 'failed_logins' in request.session:
    request.session['failed_logins'] = request.session.get('failed_logins', 0) + 1
view = render(request, 'main/login.html', {
    'page_title': PAGE_TITLE,
    'failed': request.session['failed_logins']
})

在我的本地主机和服务器上,failed{{ failed }} 在模板中呈现时显示为空。这是意料之中的,它在第一次加载时不存在。

但是在 localhost 上,这会导致 {% if failed &lt; 5 %} 显然评估为 false,因为未显示该消息。这对我来说很有意义,因为它可以在 localhost 上进行测试,所以我认为它是已定义的行为。

在服务器 (AWS) 上,尽管 {% failed &lt; 5 %} 似乎评估为 true。显示消息。这对我来说很有意义,因为也许None 被认为比什么都少。但它让我感到困惑,因为它在本地服务器上以一种方式运行,而在远程服务器上以另一种方式运行。

什么可能导致这种差异?

【问题讨论】:

    标签: python django python-3.x


    【解决方案1】:

    您在本地使用 Python 2,在服务器上使用 Python 3?评估 None 的方式发生了变化:

    https://docs.python.org/3/whatsnew/3.0.html#ordering-comparisons

    【讨论】:

    • 发帖后就想到了。反之亦然。我在本地有 python 3,在机器上有 python 2。我现在正在寻找如何强制uwsgi 使用我刚刚安装在服务器上的python 3。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 2016-05-20
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 2020-11-04
    • 2019-10-31
    相关资源
    最近更新 更多