OperationalError:(sqlite3.OperationalError)没有这样的表:user

时间:2015-11-18 15:36:15

标签: python flask sqlite steam-web-api

我对瓶子和网络开发一般都是新手。我需要的是使用steam id登录网站。我正在按照here说的那样做,但得到以下错误:

  

OperationalError:(sqlite3.OperationalError)没有这样的表:user

似乎正确打开了蒸汽网站,但是当我按 登录 时,它会中断。那么,我的错误是什么?任何帮助表示赞赏。 代码:

from flask import Flask, render_template, redirect, session, json, g
from flask_bootstrap import Bootstrap
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.openid import OpenID
import urllib
import re

app = Flask(__name__)
app.secret_key = '123'
Bootstrap(app)
app.config.from_pyfile('settings.cfg')
db = SQLAlchemy(app)
oid = OpenID(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    steam_id = db.Column(db.String(40))
    nickname = db.String(80)

    @staticmethod
    def get_or_create(steam_id):
        rv = User.query.filter_by(steam_id=steam_id).first()
        if rv is None:
            rv = User()
            rv.steam_id = steam_id
            db.session.add(rv)
        return rv

def get_steam_userinfo(steam_id):
    options = {
        'key': app.config['STEAM_API_KEY'],
        'steamids': steam_id
    }
    url = 'http://api.steampowered.com/ISteamUser/' \
          'GetPlayerSummaries/v0001/?%s' % urllib.urlencode(options)
    rv = json.load(urllib.urlopen(url))
    return rv['response']['players']['player'][0] or {}

_steam_id_re = re.compile('steamcommunity.com/openid/id/(.*?)$')

@app.route('/login')
@oid.loginhandler
def login():
    if g.user is not None:
        return redirect(oid.get_next_url())
    return oid.try_login('http://steamcommunity.com/openid')

@oid.after_login
def create_or_login(resp):
    match = _steam_id_re.search(resp.identity_url)
    g.user = User.get_or_create(match.group(1))
    steamdata = get_steam_userinfo(g.user.steam_id)
    g.user.nickname = steamdata['personaname']
    db.session.commit()
    session['user_id'] = g.user.id
    flash('You are logged in as %s' % g.user.nickname)
    return redirect(oid.get_next_url())

@app.before_request
def before_request():
    g.user = None
    if 'user_id' in session:
        g.user = User.query.get(session['user_id'])

@app.route('/')
def homepage():
    return render_template('mainpage.html')

@app.route('/logout')
def logout():
    session.pop('user_id', None)
    return redirect(oid.get_next_url())

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

1 个答案:

答案 0 :(得分:0)

您需要在运行应用之前运行db.create_all()。 这将创建模型在数据库中描述的所有表。

如果您不熟悉烧瓶,可以按照快速入门提示here

进行操作