Flask 应用程序不与包含的文件对话

Flask app does not talk to included files

我正在学习教程 here

我的 nginx 文件:

server {
    listen 80 default_server;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri @application;
    }
    location @application {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/uwsgi.sock;
    }
}

终端的东西 - 在激活 virtualenv 之后:

uwsgi -s /tmp/uwsgi.sock --module runserver --callable app --enable-threads --uid www-data --gid www-data

猫runserver.py

from __init__ import app

import routes

if __name__ == '__main__':
    app.debug = True
    app.secret_key = "BuckyCaptainAmericaHydraStarkTonyWidowBlack"
    app.run(host='0.0.0.0', port=8080)

猫routes.py

from flask import Flask, render_template,request, session, redirect, url_for, flash, make_response
import datetime
import json
from bson.json_util import dumps
from controller import actionController
from controller import dataController
from controller import usersController
from controller import authController
import base64
from flask.ext.paginate import Pagination
from __init__ import app

@app.route('/')
def index():
        return "<h1>Hello World</h1>"

这很完美!

但是,而不是 Hello World.. 如果做常规的事情。

@app.route('/')
def index():
        if 'loggedin' in session:
                authController.Auth.forward()

它抛出内部服务器错误。

当我删除配置并执行时:

python3 runserver.py

完全没有问题,通过 shell!

启动时效果惊人

因此,如果我调用包含的文件(通过导入),并使用 uwsgi,它们将停止工作。

Flask 使用密钥对会话 cookie 进行签名。您可以像这样为其分配一个值:

if __name__ == '__main__':
    # snip
    app.secret_key = "BuckyCaptainAmericaHydraStarkTonyWidowBlack"
    # snip

当你直接执行runserver.py时,这个块只有运行秒。这就是为什么

$ python3 runserver.py

有效。但是,当您 运行 您的应用程序通过 uWSGI 时,检查结果为假并且永远不会设置密钥。

要解决此问题,请将 app.secret_key = ... 移到 if 块之外。更好的是,将其移动到实例化 app__init__.py。这样您的应用程序将始终拥有其密钥。