我相信我已经将我的问题缩小到如何使用SQLAlchemy在MariaDB中设置行数据。我一直在撞墙,在互联网上寻找一种方法让这个错误消失并改变我的数据库条目。
这是我第一次使用SQLAlchemy。我得到了edit.html页面,但是当我进行更改并点击"完成"我收到以下错误。
错误:
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
不在此网站上的朋友帮我找到了这个问题。它实际上是在HTML代码中。
错:
form action="edit" method="POST"
正确:
form action="/edit/{{ post.id }}" method="POST"
所以即使我可以从数据库中提取数据,帖子也试图编辑一个不存在的SQLAlchemy项目。