Flask post 数据到 table 使用 SQLAlchemy/Mysql
Flask post data to table using SQLAlchemy/Mysql
我正在尝试在 Pythonanywhere 上使用 Flask/Python/SQLAlchemy 从 html 表单向 MySQL table 写入数据。 Mysql table 已设置,我可以在 /transactions 上输入数据,但随后它崩溃了...
Mysql:
mysql> DESCRIBE transactions1;
+----------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------+------+-----+---------+----------------+
| trans_id | int(11) | NO | PRI | NULL | auto_increment |
| Item | varchar(4096) | YES | | NULL | |
| Shack | varchar(4096) | YES | | NULL | |
| Paym_Reference | varchar(20) | YES | | NULL | |
| Amount | varchar(10) | YES | | NULL | |
+----------------+---------------+------+-----+---------+----------------+
HTML:'Transactions.html':
{% extends "template3.html" %}
{% block content %}
<h1>Enter a Transaction</h1>
<form action="", method="POST">
<dl>
<dt>Sale Item:
<dd><input type=text name="Item" required/>
<dt>Shack:
<dd><input type=text name="Shack" required/>
<dt>Reference:
<dd><input type=text name="Paym_Reference" required/>
<dt>Amount:
<dd><input type=text name="Amount" required/>
</dl)
<br>
<p><input type=submit value=Enter>
</form>
{% endblock %}
代码:
from flask import Flask, render_template, request, redirect, url_for, session, flash
import sys
from functools import wraps
from flask.ext.sqlalchemy import SQLAlchemy
SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
username="username",
password="password",
hostname="hostname",
databasename="databsename",
)
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
db = SQLAlchemy(app)
class Transactions1(db.Model):
__tablename__ = "transactions1"
trans_id = db.Column(db.Integer, primary_key=True)
Item = db.Column(db.String(4096))
Shack = db.Column(db.String(4096))
Paym_Reference = db.Column(db.VARCHAR(20))
Amount = db.Column(db.VARCHAR(10))
@app.route('/transactions', methods=['GET', 'POST'])
def transactions():
if request.method == 'GET':
return render_template('Transactions.html')
sale = transactions1(content=request.form["Item"])
sale1 = transactions1(content=request.form["Shack"])
sale2 = transactions1(content=request.form["Paym_Reference"])
sale3 = transactions1(content=request.form["Amount"])
db.session.add(sale)
db.session.add(sale1)
db.session.add(sale2)
db.session.add(sale3)
db.session.commit()
return render_template('Transactions.html')
完整错误:
2017-03-03 12:26:03,396 :Error running WSGI application
2017-03-03 12:26:03,398 :NameError: global name 'transactions1' is not defined
2017-03-03 12:26:03,398 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2000, in __call__
2017-03-03 12:26:03,399 : return self.wsgi_app(environ, start_response)
2017-03-03 12:26:03,399 :
2017-03-03 12:26:03,399 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1991, in wsgi_app
2017-03-03 12:26:03,399 : response = self.make_response(self.handle_exception(e))
2017-03-03 12:26:03,399 :
2017-03-03 12:26:03,399 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1567, in handle_exception
2017-03-03 12:26:03,399 : reraise(exc_type, exc_value, tb)
2017-03-03 12:26:03,399 :
2017-03-03 12:26:03,399 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
2017-03-03 12:26:03,399 : response = self.full_dispatch_request()
2017-03-03 12:26:03,399 :
2017-03-03 12:26:03,400 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request
2017-03-03 12:26:03,400 : rv = self.handle_user_exception(e)
2017-03-03 12:26:03,400 :
2017-03-03 12:26:03,400 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception
2017-03-03 12:26:03,400 : reraise(exc_type, exc_value, tb)
2017-03-03 12:26:03,400 :
2017-03-03 12:26:03,400 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request
2017-03-03 12:26:03,400 : rv = self.dispatch_request()
2017-03-03 12:26:03,400 :
2017-03-03 12:26:03,400 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request
2017-03-03 12:26:03,400 : return self.view_functions[rule.endpoint](**req.view_args)
2017-03-03 12:26:03,400 :
2017-03-03 12:26:03,401 : File "/home/SIIL/mysite/flask_app.py", line 64, in transactions
2017-03-03 12:26:03,401 : sale = transactions1(content=request.form["Item"])
您可以通过表名调用模型。
你的代码应该喜欢这样..而不是transactions1
@app.route('/transactions', methods=['GET', 'POST'])
def transactions():
if request.method == 'GET':
return render_template('Transactions.html')
sale = Transactions1(Item=request.form["Item"],Shack=request.form["Shack"],Paym_Reference=request.form["Paym_Reference"],Amount=request.form["Amount"])
db.session.add(sale)
db.session.commit()
return render_template('Transactions.html')
我正在尝试在 Pythonanywhere 上使用 Flask/Python/SQLAlchemy 从 html 表单向 MySQL table 写入数据。 Mysql table 已设置,我可以在 /transactions 上输入数据,但随后它崩溃了...
Mysql:
mysql> DESCRIBE transactions1;
+----------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------+------+-----+---------+----------------+
| trans_id | int(11) | NO | PRI | NULL | auto_increment |
| Item | varchar(4096) | YES | | NULL | |
| Shack | varchar(4096) | YES | | NULL | |
| Paym_Reference | varchar(20) | YES | | NULL | |
| Amount | varchar(10) | YES | | NULL | |
+----------------+---------------+------+-----+---------+----------------+
HTML:'Transactions.html':
{% extends "template3.html" %}
{% block content %}
<h1>Enter a Transaction</h1>
<form action="", method="POST">
<dl>
<dt>Sale Item:
<dd><input type=text name="Item" required/>
<dt>Shack:
<dd><input type=text name="Shack" required/>
<dt>Reference:
<dd><input type=text name="Paym_Reference" required/>
<dt>Amount:
<dd><input type=text name="Amount" required/>
</dl)
<br>
<p><input type=submit value=Enter>
</form>
{% endblock %}
代码:
from flask import Flask, render_template, request, redirect, url_for, session, flash
import sys
from functools import wraps
from flask.ext.sqlalchemy import SQLAlchemy
SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
username="username",
password="password",
hostname="hostname",
databasename="databsename",
)
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
db = SQLAlchemy(app)
class Transactions1(db.Model):
__tablename__ = "transactions1"
trans_id = db.Column(db.Integer, primary_key=True)
Item = db.Column(db.String(4096))
Shack = db.Column(db.String(4096))
Paym_Reference = db.Column(db.VARCHAR(20))
Amount = db.Column(db.VARCHAR(10))
@app.route('/transactions', methods=['GET', 'POST'])
def transactions():
if request.method == 'GET':
return render_template('Transactions.html')
sale = transactions1(content=request.form["Item"])
sale1 = transactions1(content=request.form["Shack"])
sale2 = transactions1(content=request.form["Paym_Reference"])
sale3 = transactions1(content=request.form["Amount"])
db.session.add(sale)
db.session.add(sale1)
db.session.add(sale2)
db.session.add(sale3)
db.session.commit()
return render_template('Transactions.html')
完整错误:
2017-03-03 12:26:03,396 :Error running WSGI application
2017-03-03 12:26:03,398 :NameError: global name 'transactions1' is not defined
2017-03-03 12:26:03,398 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2000, in __call__
2017-03-03 12:26:03,399 : return self.wsgi_app(environ, start_response)
2017-03-03 12:26:03,399 :
2017-03-03 12:26:03,399 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1991, in wsgi_app
2017-03-03 12:26:03,399 : response = self.make_response(self.handle_exception(e))
2017-03-03 12:26:03,399 :
2017-03-03 12:26:03,399 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1567, in handle_exception
2017-03-03 12:26:03,399 : reraise(exc_type, exc_value, tb)
2017-03-03 12:26:03,399 :
2017-03-03 12:26:03,399 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
2017-03-03 12:26:03,399 : response = self.full_dispatch_request()
2017-03-03 12:26:03,399 :
2017-03-03 12:26:03,400 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request
2017-03-03 12:26:03,400 : rv = self.handle_user_exception(e)
2017-03-03 12:26:03,400 :
2017-03-03 12:26:03,400 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception
2017-03-03 12:26:03,400 : reraise(exc_type, exc_value, tb)
2017-03-03 12:26:03,400 :
2017-03-03 12:26:03,400 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request
2017-03-03 12:26:03,400 : rv = self.dispatch_request()
2017-03-03 12:26:03,400 :
2017-03-03 12:26:03,400 : File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request
2017-03-03 12:26:03,400 : return self.view_functions[rule.endpoint](**req.view_args)
2017-03-03 12:26:03,400 :
2017-03-03 12:26:03,401 : File "/home/SIIL/mysite/flask_app.py", line 64, in transactions
2017-03-03 12:26:03,401 : sale = transactions1(content=request.form["Item"])
您可以通过表名调用模型。
你的代码应该喜欢这样..而不是transactions1
@app.route('/transactions', methods=['GET', 'POST'])
def transactions():
if request.method == 'GET':
return render_template('Transactions.html')
sale = Transactions1(Item=request.form["Item"],Shack=request.form["Shack"],Paym_Reference=request.form["Paym_Reference"],Amount=request.form["Amount"])
db.session.add(sale)
db.session.commit()
return render_template('Transactions.html')