部署到Heroku的Flask-Pymongo应用程序出现服务器错误....我错过了什么?

时间:2018-04-28 02:45:39

标签: python heroku flask pymongo

我会尝试在这里提供尽可能多的信息。该应用程序在本地工作正常。该应用程序已成功部署,我可以进入登录屏幕...但是当我登录时,我看到以下错误: Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

我为疯狂的长篇帖子道歉,但我想确保我得到的所有东西都可能有用......

我当前的文件结构:

-static
--client side js, css, images etc
-templates
--all my templates
-app.py
-Pipfile
-Pipfile.lock
-Procfile
-requirements.txt

app.py:

from flask import Flask, render_template, url_for, request, session, redirect, jsonify, json, flash
from flask_pymongo import PyMongo
from jinja2 import Template, TemplateNotFound
from bson.json_util import dumps
from bson.objectid import ObjectId
import pprint

app = Flask(__name__)


app.config['MONGO_DBNAME'] = 'heroku_fbdc95hs'
app.config['MONGO_URI'] = 'mongodb://<username>:<password>@ds161529.mlab.com:61529/heroku_fbdc95hs'

mongo = PyMongo(app)


@app.route("/")
def index():
    if 'username' in session:
        return render_template('dashboard.html')

    return render_template('index.html')

@app.route("/dashboard")
def dashboard():
    if 'username' not in session:
        return render_template('index.html')

    return render_template('dashboard.html')

@app.route("/student/<string:student_id>")
def student(student_id):
    if 'username' not in session:
        return render_template('index.html')

    print (student_id)
    students = mongo.db.students
    current_student = students.find_one({'_id' : ObjectId(student_id)})
    print(current_student)
    return render_template('student.html', current_student=current_student)


@app.route("/find_all")
def find_all():
    if 'username' not in session:
        return render_template('index.html')

    students = mongo.db.students.find()
    return render_template('view-students.html', students=students)

@app.route("/find_ee")
def find_ee():
    if 'username' not in session:
        return render_template('index.html')

    students = mongo.db.students.find({"major" : "B.S. Electrical Engineering" })
    return render_template('view-students.html', students=students)


@app.route("/find_ce")
def find_ce():
    if 'username' not in session:
        return render_template('index.html')

    students = mongo.db.students.find({"major" : "B.S. Computer Engineering" })
    return render_template('view-students.html', students=students)

@app.route("/probation")
def probation():
    if 'username' not in session:
        return render_template('index.html')

    students = mongo.db.students.find({"standing" : "Probation" })
    return render_template('view-students.html', students=students)



@app.route("/find_uid", methods=['GET', 'POST'])

def find_uid():
    if 'username' not in session:
        return render_template('index.html')

    if request.method == 'POST':
        result = request.form['uid-input']
        students = mongo.db.students.find({"student_id" : result })

    return render_template('view-students.html', students=students)

@app.route("/view-students")
def view_students():
    if 'username' not in session:
        return render_template('index.html')

    return render_template('view-students.html')

@app.route("/add-student")
def add_student():
    if 'username' not in session:
        return render_template('index.html')


    return render_template('add-student.html')


@app.route('/login', methods=['POST'])
def login():
    users = mongo.db.users
    login_user = users.find_one({'username' : request.form['username']})

    if login_user:
        if request.form['password'] == login_user['password']:
            session['username'] = request.form['username']
            return redirect(url_for('dashboard'))
        return 'Invalid username/password combination'
    return 'Invalid username/password combination'

@app.route('/add', methods=['GET','POST'])
def add():
    if request.method == 'POST':
        students = mongo.db.students
        existing_student = students.find_one({'student_id' : request.form['student_id']})

        if existing_student is None:
            students.insert({'first_name' : request.form['first_name'],
                'last_name' : request.form['last_name'],
                'student_id' : request.form['student_id'],
                'major' : request.form['major'],
                'standing' : request.form['standing'],
                'email' : request.form['email'],
                'phone' : request.form['phone'],
                'transfer' : request.form['transfer']})
            flash("Student successfully added!")
            return redirect(url_for('add'))

        flash("A student with that ID already exists!")


    return render_template('add-student.html')


@app.route('/logout')
def logout():
    session.clear()
    return redirect(url_for('index'))


if __name__ == "__main__":
    app.secret_key = 'mysecret'
    app.run(debug=True)

Pipfile:

[requires]
python_full_version = "2.7.14"

Procfile:

web: gunicorn --bind 0.0.0.0:$PORT app:app

日志:

2018-04-28T02:37:03.799600+00:00 heroku[router]: at=info method=GET path="/dashboard" host=studentsrm.herokuapp.com request_id=c303aed1-a45e-4b84-912a-755fc582c0a5 fwd="73.98.161.187" dyno=web.1 connect=0ms service=3ms status=200 bytes=2582 protocol=https
2018-04-28T02:37:03.794207+00:00 app[web.1]: 10.151.201.12 - - [28/Apr/2018:02:37:03 +0000] "GET /dashboard HTTP/1.1" 200 2420 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
2018-04-28T02:37:14.577644+00:00 heroku[router]: at=info method=POST path="/login" host=studentsrm.herokuapp.com request_id=ae63c35d-d93f-4240-bbdd-10306d31961b fwd="73.98.161.187" dyno=web.1 connect=1ms service=12ms status=500 bytes=456 protocol=https
2018-04-28T02:37:14.570254+00:00 app[web.1]: [2018-04-28 02:37:14,569] ERROR in app: Exception on /login [POST]
2018-04-28T02:37:14.570268+00:00 app[web.1]: Traceback (most recent call last):
2018-04-28T02:37:14.570274+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
2018-04-28T02:37:14.570275+00:00 app[web.1]:     response = self.full_dispatch_request()
2018-04-28T02:37:14.570277+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
2018-04-28T02:37:14.570279+00:00 app[web.1]:     rv = self.handle_user_exception(e)
2018-04-28T02:37:14.570281+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
2018-04-28T02:37:14.570282+00:00 app[web.1]:     reraise(exc_type, exc_value, tb)
2018-04-28T02:37:14.570284+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
2018-04-28T02:37:14.570286+00:00 app[web.1]:     rv = self.dispatch_request()
2018-04-28T02:37:14.570287+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
2018-04-28T02:37:14.570289+00:00 app[web.1]:     return self.view_functions[rule.endpoint](**req.view_args)
2018-04-28T02:37:14.570291+00:00 app[web.1]:   File "/app/app.py", line 113, in login
2018-04-28T02:37:14.570293+00:00 app[web.1]:     session['username'] = request.form['username']
2018-04-28T02:37:14.570294+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/werkzeug/local.py", line 350, in __setitem__
2018-04-28T02:37:14.570296+00:00 app[web.1]:     self._get_current_object()[key] = value
2018-04-28T02:37:14.570298+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/sessions.py", line 130, in _fail
2018-04-28T02:37:14.570299+00:00 app[web.1]:     raise RuntimeError('The session is unavailable because no secret '
2018-04-28T02:37:14.570302+00:00 app[web.1]: RuntimeError: The session is unavailable because no secret key was set.  Set the secret_key on the application to something unique and secret.
2018-04-28T02:37:14.572619+00:00 app[web.1]: 10.151.201.12 - - [28/Apr/2018:02:37:14 +0000] "POST /login HTTP/1.1" 500 291 "https://studentsrm.herokuapp.com/dashboard" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"

1 个答案:

答案 0 :(得分:0)

试试这个: -

app.secret_key =&#34;定义您自己选择的密钥&#34;

我希望这对你有用