Flask post data to table using SQLAlchemy/Mysql [closed]

五迷三道 提交于 2019-12-13 21:26:23

问题


I am trying to write data to a MySQL table from a html form, using Flask/Python/SQLAlchemy on Pythonanywhere. Mysql table is setup, I can enter data on the /transactions, but then it crashes...

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 %}

Code:

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

Full error:

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"])

回答1:


You can call Model by tablename .

you code should like this .. instead of 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')


来源:https://stackoverflow.com/questions/42579079/flask-post-data-to-table-using-sqlalchemy-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!