My code is as below
# Cafe TABLE Configuration
class Cafe(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(250), unique=True, nullable=False)
map_url = db.Column(db.String(500), nullable=False)
img_url = db.Column(db.String(500), nullable=False)
location = db.Column(db.String(250), nullable=False)
seats = db.Column(db.String(250), nullable=False)
has_toilet = db.Column(db.Boolean, nullable=False)
has_wifi = db.Column(db.Boolean, nullable=False)
has_sockets = db.Column(db.Boolean, nullable=False)
can_take_calls = db.Column(db.Boolean, nullable=False)
coffee_price = db.Column(db.String(250), nullable=True)
HTTP POST - Create Record
app.route('/add_cafe', methods=['POST', 'GET'])
def add_cafe():
new_data = Cafe(name=request.form.get("name"),
map_url=request.form.get("map_url"),
img_url=request.form.get("img_url"),
location=request.form.get("location"),
has_sockets=bool(int(request.form.get("has_sockets"))),
has_toilet=bool(int(request.form.get("has_toilet"))),
has_wifi=bool(int(request.form.get("has_wifi"))),
can_take_calls=bool(int(request.form.get("can_take_calls"))),
seats=request.form.get("seats"),
coffee_price=request.form.get("coffee_price")),
new_cafe_json = jsonify(cafe=new_data)
db.session.add(new_cafe_json)
db.session.commit()
return jsonify(result={"Success": "Added successfully"})
I am getting 404 error - not found. Other requests are working fine. Any help. I am a beginner.
CodePudding user response:
It seems you arent decorating the view function correctly.
your code is like this:
app.route('/add_cafe', methods=['POST', 'GET'])
def add_cafe():
new_data = Cafe(name=request.form.get("name"),
map_url=request.form.get("map_url"),
.....
And it should be like:
@app.route('/add_cafe', methods=['POST', 'GET'])
def add_cafe():
your code...
It seems that your usage of SQLAlchemy isnt in good shape as well. In the part you add information to db.session
you should add a Model Instance of the object you want to save, in your code the object is the variable new_data
like this: db.session.add(new_data)
and then commit.
Greetings!