Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError, 'dict' object has no attribute 'iteritems'; Flask-SQLAlchemy error while committing to database

Tags:

python

flask

I've been trying to get this to run for a while but seemingly to no avail. I have a webapp that requests the user input data, which is then committed to the db. When I hit add through the browser, the data is passed to the db however Werkzeug spits a number of errors back, which I've detailed below in the trace.

My 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'))

The trace from the error:

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'
like image 321
user92466 Avatar asked Jun 14 '16 05:06

user92466


1 Answers

In Python3 dict.items() does the same thing that dict.iteritems() did in Python2. Just replace iteritems() with items()!

like image 75
TheLazyScripter Avatar answered Sep 28 '22 17:09

TheLazyScripter