0

I believe I have narrowed down my issue to how I'm setting the row data in MariaDB with SQLAlchemy. I've been banging my head against the wall and searching the internet for a way to get this error to go away and change my database entries.

This is my first time using SQLAlchemy. I get the edit.html page to serve fine but when I make changes and click "finish" I get the following error.

Error:

127.0.0.1 - - [02/Mar/2017 13:26:19] "POST /edit/edit HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/cpuppe/it-equip-db/venv/lib/python2.7/site-packages/flask/app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/cpuppe/it-equip-db/venv/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/home/cpuppe/it-equip-db/venv/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/cpuppe/it-equip-db/venv/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/cpuppe/it-equip-db/venv/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/cpuppe/it-equip-db/venv/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/cpuppe/it-equip-db/venv/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/cpuppe/it-equip-db/venv/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/cpuppe/it-equip-db/app/views.py", line 28, in edit
    post.name = request.form['name']
AttributeError: 'NoneType' object has no attribute 'name'
127.0.0.1 - - [02/Mar/2017 13:26:19] "GET /edit/edit?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [02/Mar/2017 13:26:19] "GET /edit/edit?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [02/Mar/2017 13:26:19] "GET /edit/edit?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [02/Mar/2017 13:26:20] "GET /edit/edit?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
127.0.0.1 - - [02/Mar/2017 13:26:20] "GET /edit/edit?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [02/Mar/2017 13:26:20] "GET /edit/edit?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -

views.py

from flask import render_template, request, flash, redirect, url_for
from app import app, db
from app.models import *

@app.route('/edit/<id>' , methods=['POST', 'GET'])
def edit(id):
    #Getting user by primary key:
    post = Employee.query.get(id)
    if request.method == 'POST':
        print request.form['name']
        post.name = request.form['name']
        post.create_ts = request.form['create_ts']
        post.skill_level = request.form['skill_level']
        post.email_address = request.form['email_address']
        post.trade = request.form['trade']
        db.session.commit()
        return  redirect(url_for('index'))
    return render_template('edit.html', post=post)

models.py

from app import db
from sqlalchemy.dialects.mysql import ENUM, BOOLEAN

class Employee(db.Model):
    __tablename__ = 'employee'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), unique=True)
    create_ts = db.Column(db.DateTime)
    skill_level = db.Column(db.Integer)
    email_address = db.Column(db.String(128))
    trade = db.Column(db.String(128))

    def __init__(self, id, name, create_ts, skill_level, email_address, trade):
        self.id = id
        self.name = name
        self.create_ts = create_ts
        self.skill_level = skill_level
        self.email_address = email_address
        self.trade = trade

A friend not on this site helped me find the issue. And it was actually in the HTML code.

Wrong:

form action="edit" method="POST"

Correct:

form action="/edit/{{ post.id }}" method="POST"

So even though I could pull data from the the database the post was trying to edit an SQLAlchemy item that didn't exist.

Vanilla Nice
  • 43
  • 1
  • 7
  • 2
    Please do not vandalize your posts. Once you've posted a question, it belongs to the Stack Overflow community at large (under the CC-by-SA license). If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](http://meta.stackoverflow.com/questions/323395/what-is-the-proper-rout‌​e-for-a-dissociation-request) – DavidPostill Mar 02 '17 at 22:07
  • Answer in the post. – Vanilla Nice Mar 06 '17 at 17:21

0 Answers0