Re: [flask] What's wrong with my code or server?
- From:
- Zach Williams
- Date:
- 2011-05-19 @ 19:23
Haven't tested this, but:
Maybe you should be importing "from sqlite3 import dbapi2 as sqlite3"?
Also, you might need to use "@app.before_request" to add "g.db =
connect_db()"?
Check out the Github flaskr.py:
https://github.com/mitsuhiko/flask/blob/master/examples/flaskr/flaskr.py
On Thu, May 19, 2011 at 2:07 PM, Tony Wang <ivytony@gmail.com> wrote:
> Hi guys,
>
> I am a beginner in learning using Flask. I have a VPS built with CentOS 5.6
> 64 bit, Python 2.7, Apache + mod_wsgi.
>
> Basically, I am following this Quickstart tutorial (
> http://flask.pocoo.org/docs/quickstart/) to write my first Flask app. I am
> now running into some problems when I try to access http://localhost:5000.
> Below is the errors:
>
> AttributeError
>>
>> AttributeError: '_RequestGlobals' object has no attribute 'db'
>> Traceback *(most recent call last)*
>>
>> - File "/opt/python2.7/lib/python2.7/site-packages/flask/app.py", line
>> *889*, in __call__
>>
>> return self.wsgi_app(environ, start_response)
>>
>> - File "/opt/python2.7/lib/python2.7/site-packages/flask/app.py", line
>> *879*, in wsgi_app
>>
>> response = self.make_response(self.handle_exception(e))
>>
>> - File "/opt/python2.7/lib/python2.7/site-packages/flask/app.py", line
>> *876*, in wsgi_app
>>
>> rv = self.dispatch_request()
>>
>> - File "/opt/python2.7/lib/python2.7/site-packages/flask/app.py", line
>> *695*, in dispatch_request
>>
>> return self.view_functions[rule.endpoint](**req.view_args)
>>
>> - File "/opt/web/flask/flaskr.py", line *29*, in show_entries
>>
>> cur = g.db.execute('select title, text from entries order by id desc')
>>
>> - File "/opt/python2.7/lib/python2.7/site-packages/werkzeug/local.py",
>> line *347*, in __getattr__
>>
>> return getattr(self._get_current_object(), name)
>>
>>
>> AttributeError: '_RequestGlobals' object has no attribute 'db'
>>
>>
>>
>>
> Below is the content of my flaskr.py file:
>
> 1 # all the imports 2 from __future__ import with_statement 3 import
>> sqlite3 4 from flask import Flask, request, session, g, redirect,
>> url_for, abort, render_template, flash 5 from contextlib import closing
>> 6
>> 7 # configuration 8 DATABASE = '/tmp/flaskr.db' 9 DEBUG = True 10
SECRET_KEY
>> = 'admin' 11 USERNAME = 'admin' 12 PASSWORD = 'admin' 13 # create our
>> little application :) 14 app = Flask(__name__) 15
>> app.config.from_object(__name__) 16
>> 17 def init_db(): 18 with closing(connect_db()) as db: 19 with
>> app.open_resource('schema.sql') as f: 20
>> db.cursor().executescript(f.read()) 21 db.commit() 22
>> 23 #@app.route('/') 24 def connect_db(): 25 return
>> sqlite3.connect(app.config['DATABASE']) 26
>> 27 @app.route('/') 28 def show_entries(): 29 cur =
>> g.db.execute('select title, text from entries order by id desc') 30 entries
>> = [dict(title=row[0], text=row[1]) for row in cur.fetchall()] 31 return
>> render_template('show_entries.html', entries=entries) 32
>> 33 @app.route('/add', methods=['POST']) 34 def add_entry(): 35 if not
>> session.get('logged_in'): 36 abort(401) 37 g.db.execute('insert into
>> entries (title, text) values (?, ?)', 38 [request.form['title'],
>> request.form['text']]) 39 g.db.commit() 40 flash('New entry was
>> successfully posted') 41 return redirect(url_for('show_entries')) 42
>> 43 @app.route('/login', methods=['GET', 'POST']) 44 def login(): 45 error
>> = None 46 if request.method == 'POST': 47 if request.form['username']
>> != app.config['USERNAME']: 48 error = 'Invalid username' 49 elif
>> request.form['password'] != app.config['PASSWORD']: 50 error = 'Invalid
>> password' 51 else: 52 session['logged_in'] = True 53 flash('You were
>> logged in') 54 return redirect(url_for('show_entries')) 55 return
>> render_template('login.html', error=error) 56
>> 57 @app.route('/logout') 58 def logout(): 59 session.pop('logged_in',
>> None) 60 flash('You were logged out') 61 return
>> redirect(url_for('show_entries')) 62
>> 63 if __name__ == '__main__': 64 app.run()
>
>
> Could anybody give me some quick help and point me to where I should do the
> correction? Thanks a lot!
>
> tony
> --
> - Are you RCholic? www.RCholic.com
> - 温 良 恭 俭 让 仁 义 礼 智 信
> - Through suffering to renown
>
>
Re: [flask] What's wrong with my code or server?
- From:
- Steven Kryskalla
- Date:
- 2011-05-19 @ 19:21
On Thu, May 19, 2011 at 3:07 PM, Tony Wang <ivytony@gmail.com> wrote:
> Hi guys,
>
> I am a beginner in learning using Flask. I have a VPS built with CentOS
5.6 64 bit, Python 2.7, Apache + mod_wsgi.
>
> Basically, I am following this Quickstart tutorial
(http://flask.pocoo.org/docs/quickstart/) to write my first Flask app. I
am now running into some problems when I try to access
http://localhost:5000. Below is the errors:
>
>> AttributeError
>> AttributeError: '_RequestGlobals' object has no attribute 'db'
>>
> Could anybody give me some quick help and point me to where I should do
the correction? Thanks a lot!
You are trying to use 'db' in your view before it's been connected and
placed into the globals "g". You need to follow the tutorial more
closely, this step:
http://flask.pocoo.org/docs/tutorial/dbcon/#tutorial-dbcon
Or take a look at the fully working code to see what you are missing:
https://github.com/mitsuhiko/flask/blob/master/examples/flaskr/flaskr.py
-Steve