Flask服务器无法与CodernityDB一起正常工作

2024-09-28 22:28:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试完成this和{a2}之间的Flask教程,以便使用CodernityDB而不是sqllite实现一些非常简单的事情,我需要执行该应用程序来研究我需要的所有CodernityDB方法。但是服务器不能正常工作,本地主机告诉我有一个内部错误,但我无法找到调试它的方法。在

这是我的代码(烧瓶),模板是here

from __future__ import with_statement
from flask import Flask, request, session, g, redirect, url_for, \
     abort, render_template, flash
from CodernityDB.database_thread_safe import ThreadSafeDatabase
from CodernityDB.database import RecordNotFound

# configuration
DATABASE = '/tmp/flaskr.db'
DEBUG = True
SECRET_KEY = 'development key'
# pending
USER = 'admin'
PASSWORD = 'default'

app = Flask(__name__)
##app.config.from_object(__name__)

cdb = ThreadSafeDatabase(DATABASE)

if cdb.exists():
    cdb.open()
    cdb.reindex()
else:
    from database_indexes import WithXIndex
    cdb.create()
    cdb.add_index(WithXIndex(cdb.path, 'x'))

@app.before_request
def before_request():
    g.db = cdb

@app.route('/')
def show_entries():
##    return "Hello World! This is powered by Python Backend."
    cur = db.get('x',10,with_doc=True)
    entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
    return render_template('show_entries.html', entries=entries)

@app.route('/add', methods=['POST'])
def add_entry():
##    return "Add new entry"
    if not session.get('logged_in'):
        abort(401)
    g.db.insert(dict(x=request.form['title'], name=request.form['text']))
    flash('New entrey was succesfully posted')
    return redirect(url_for('show_entries'))

@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] != app.config['USERNAME']:
            error = 'Invalid username'
        elif request.form['password'] != app.config['PASSWORD']:
            error = 'Invalid password'
        else:
            session['logged_in'] = True
            flash('You were logged in')
            return redirect(url_for('show_entries'))
    return render_template('login.html', error=error)

@app.route('/logout')
def logout():
    session.pop('logged_in', None)
    flash('You were logged out')
    return redirect(url_for('show_entries'))

if __name__ == '__main__':
    app.run()
##    app.run(host='0.0.0.0')
##    app.run(debug= True)
##    app.run(host='127.0.0.1', port=5000)

数据库_索引.py

^{pr2}$

我没有创建环境变量,因为我想使用另一种配置方法,可能是一个文件,以及config.from_对象是注释的,因为当我用它执行它时,它显示用stat重新启动


Tags: infromimportappforreturnifrequest