在win7上用烧瓶设置postgres [英] Setting up postgres with flask on win7

查看:181
本文介绍了在win7上用烧瓶设置postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在开发基于 http://code.tutsplus.com/tutorials/intro-to-flask-签约入和出 - 网29982

作为tut的一部分,我试图连接到一个postgres服务器,其结构如截图所示。我已经添加了一个你可以看到的'yournewdb'表。



基于tut,我在我的主文件('routes.py')中有以下代码:

<$
$ b $从模型导入数据库
数据库。 init_app(app)

@ app.route('/ testdb')
def testdb():$ b $ if db.session.query(1)。from_statement( SELECT 1)。all():
return'It works。'
else:
return'Something is broken。'

models.py:

  from flask.ext.sqlalchemy汇入SQLAlchemy 
from werkzeug import generate_password_hash,check_password_hash
$ b db = SQLAlchemy()
$ b $ class User(db.Model):
__tablename__ ='users'
uid = db.Column(db.Integer,primary_key = True)
firstname = db.Column(db.String(100))
lastname = db.Column(db.String(100) )
email = db.Column(db.String(120),unique = True)
pwdhash = db.Column(db.String(5 4))

def __init __(self,firstname,lastname,email,password):
self.firstname = firstname.title()
self.lastname = lastname.title )
self.email = email.lower()
self.set_password(password)
$ b def set_password(self,password):
self.pwdhash = generate_password_hash(密码)

def check_password(self,password):
return check_password_hash(self.pwdhash,password)

当我去 http://127.0.0.1:5000/testdb 我得到一个内部服务器错误。调试器给出:

  C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \__init __。py:800:UserWarning:SQLALCHEMY_TRACK_MODIFICATIONS增加了大量开销,将来会被默认禁用。将其设置为True以禁止此警告。 
warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS增加了大量的开销,将来会被禁用,设置为True来取消这个警告。')
*运行于http://127.0.0.1:5000 /(按CTRL + C退出)
C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\表达式'SELECT 1'应该显式声明为文本('SELECT 1')(这个警告可能在10次出现后被抑制)
{expr:util.ellipses_string(element)})

我在做什么错?

解决方案

似乎你连接到一个表,而不是数据库,正确的?为什么不把 yournewdb 更改为 postgres 或者创建一个新的数据库?你仍然需要制作你的桌子。你可以让SQLAlchemy为你做这个。这是一个很好的答案: https://stackoverflow.com/a/20749534/2326132



我建议为每个项目建立一个新的数据库,即使它们都是测试项目。你会遇到更少的问题。


I am working on a flask app based on http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982.

As part of the tut I'm trying to connect to a postgres server, with a structure as in the screenshot. I've added a table 'yournewdb' which you can see.

Based on the tut I have the following code in my main file ('routes.py'):

app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:123@localhost/yournewdb"

from models import db
db.init_app(app)

@app.route('/testdb')
def testdb():
  if db.session.query("1").from_statement("SELECT 1").all():
    return 'It works.'
  else:
    return 'Something is broken.'

models.py:

from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug import generate_password_hash, check_password_hash

db = SQLAlchemy()

class User(db.Model):
  __tablename__ = 'users'
  uid = db.Column(db.Integer, primary_key = True)
  firstname = db.Column(db.String(100))
  lastname = db.Column(db.String(100))
  email = db.Column(db.String(120), unique=True)
  pwdhash = db.Column(db.String(54))

  def __init__(self, firstname, lastname, email, password):
    self.firstname = firstname.title()
    self.lastname = lastname.title()
    self.email = email.lower()
    self.set_password(password)

  def set_password(self, password):
    self.pwdhash = generate_password_hash(password)

  def check_password(self, password):
    return check_password_hash(self.pwdhash, password)

When I go to http://127.0.0.1:5000/testdb I'm getting an internal server error. The debugger gives:

C:\envs\virtalenvs\flask_mini\lib\site-packages\flask_sqlalchemy\__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True to suppress this warning.
  warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True to suppress this warning.')
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
C:\envs\virtalenvs\flask_mini\lib\site-packages\sqlalchemy\sql\elements.py:3779: SAWarning: Textual SQL expression 'SELECT 1' should be explicitly declared as text('SELECT 1') (this warning may be suppressed after 10 occurrences)
  {"expr": util.ellipses_string(element)})

What am I doing wrong?

解决方案

Seems you're connecting to a table, not a DB, correct? Why don't you change yournewdb to postgres or make a new DB? You'll still have to make your table. You can have SQLAlchemy do this for you. Here is a great answer on that: https://stackoverflow.com/a/20749534/2326132

I'd suggest making a new database for each project, even if they are all test projects. You'll run into fewer issues.

这篇关于在win7上用烧瓶设置postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆