我一直试图让它运行一段时间,但似乎无济于事。我有一个webapp请求用户输入数据,然后将其提交给数据库。当我通过浏览器点击添加时,数据会传递到数据库,但是Werkzeug会发回一些错误,我在下面的跟踪中详细说明了这些错误。
我的views.py:
from flask import request, flash, render_template, url_for, redirect
from aalert import app, db
import flask_whooshalchemy
from flask_login import LoginManager, login_required, logout_user
from aalert.forms import *
from aalert.models import User, PubInfo, PrivInfo
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(userid):
return User.query.filter(User.id==userid).first()
@app.route('/signup', methods=['GET', 'POST'])
def signup():
form = SignupForm()
if form.validate_on_submit():
user = User(username=form.username.data, password = form.password.data)
db.session.add(user)
db.session.commit()
return redirect(url_for('index'))
return render_template('signup.html', form=form)
#pubinfo index
@app.route('/')
def index():
entries = PubInfo.query.all()
return render_template('disp_tables.html',
entries=entries)
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first_or_404()
if user.is_correct_password(form.password.data):
login_user(user)
return redirect(url_for('add'))
else:
flash('Incorrect username and password!')
return render_template('login.html', form=form)
@app.route('/show_all')
@login_required
def show_all_data():
entries = PubInfo.query.join(PrivInfo, PubInfo.id==PrivInfo.id)
return render_template('disp_all.html',
entries)
@app.route('/add', methods=['POST', 'GET'])
#@login_required #login required to add functions, points to @login_manager.user_loader
def insert():
form = AddEntry()
if request.method == 'POST':
if not request.form['firstname'] or not request.form['lastname'] or not request.form['age'] or not request.form['height'] or not request.form['last_loc'] or not request.form['missing_since'] or not request.form['contact_info'] or not request.form['home_address']:
flash('Please ensure all fields are filled out!')
else:
pubentry = PubInfo(request.form['firstname'],
request.form['lastname'],
request.form['age'],
request.form['height'],
request.form['last_loc'],
request.form['missing_since'])
priventry = PrivInfo(request.form['contact_info'],
request.form['home_address'])
db.session.add(pubentry)
db.session.add(priventry)
db.session.commit()
flash('Record successfully added.')
return redirect(url_for('index'))
return render_template('add.html', form=form)
@app.route('/search', methods=['POST', 'GET'])
def search():
form = SearchForm()
if request.method == 'POST':
result = pubinfo.query.whoosh_search(request.form['search_query'])
return render_template('results.html', result=result)
return render_template('search.html', form=form)
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))
@app.route('/test')
def test():
return redirect(url_for('login'))
错误的痕迹:
Traceback (most recent call last):
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/scire/programs/python/aalert/aalert/views.py", line 75, in insert
db.session.commit()
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/sqlalchemy/orm/scoping.py", line 157, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/sqlalchemy/orm/session.py", line 801, in commit
self.transaction.commit()
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/sqlalchemy/orm/session.py", line 399, in commit
self.session.dispatch.after_commit(self.session)
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/sqlalchemy/event/attr.py", line 256, in __call__
fn(*args, **kw)
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 231, in after_commit
models_committed.send(session.app, changes=list(d.values()))
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/blinker/base.py", line 267, in send
for receiver in self.receivers_for(sender)]
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/blinker/base.py", line 267, in <listcomp>
for receiver in self.receivers_for(sender)]
File "/home/scire/programs/python/aalert/env/lib/python3.5/site-packages/flask_whooshalchemy.py", line 239, in _after_flush
for model, values in bytype.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'
答案 0 :(得分:4)
在Python3 dict.items()
中,dict.iteritems()
在Python2中做了同样的事情。只需将iteritems()
替换为items()
!
答案 1 :(得分:-1)
i recently encountered this same error while trying to use flask-sqlalchemy in python3 and was almost stuck. then i remember that https://blog.miguelgrinberg.com/post/about-me had done something like this before and decided to try it out
$ pip uninstall flask-whooshalchemy $ pip install git+git://github.com/miguelgrinberg/flask-whooshalchemy.git
This worked for me
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-x-full-text-search
Enjoy. ..