from flask import Flask, session
from flask import render_template
from flask_script import Manager
from flask import url_for
from flask import request, jsonify, json
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
import os
from flask_json import FlaskJSON, JsonError, json_response, as_json
from flask_marshmallow import Marshmallow
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
-sharp cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
cors = CORS(app, resources={r"/*": {"origins": "*"}})
app.config["SECRET_KEY"] = "a string"
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + \
os.path.join(basedir, "data.sqlite")
-sharp
app.config["SQLALCHEMY_COMMIT_TEARDOWN"] = True
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
manager = Manager(app)
db = SQLAlchemy(app)
ma = Marshmallow(app)
class Article(db.Model):
"""docstring for Article"""
__tablename__ = "articles"
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(64))
-sharp content = db.Column(db.Text)
content = db.Column(db.Text)
-sharp article_type = db.Column(db.String(64))
-sharp tiem = db.Column(db.DateTime)
class ArticleSchema(ma.ModelSchema):
class Meta:
medel = Article
@app.route("/")
def index():
articles = Article.query.all()
articles_schema = ArticleSchema()
output = articles_schema.dump(articles).data
return jsonify({"articles": output})
@app.route("/create", methods=["POST"])
def create():
title = request.form["title"]
content = request.form["content"]
article = Article(title=title, content=content)
db.session.add(article)
db.session.commit()
session["known"] = False
-sharp-sharpcontent
return content
if __name__ == "__main__":
manager.run()
this is the vue used in the front end to send post requests
methods:{
submitArticle: function() {
let data= {}
data["title"] = this.title
data["content"] = this.content
console.log(data)
this.$http.post("http://127.0.0.1:7626/create", data).then( res => {
console.log(res)
})
}
}
the data returned by the server is always empty (below), so it is suspected that the data was not successfully added to the database, but the data can be queried in the command line window.