0

I have a app Flask. I want to use Rest api for insert data to Database. I have a app.pyfile and api.py file. I want to write a post method in api.py. I want to get information from client using add.html file and post it to api. then api this information add to Database. I use pyodbc for connect to SQL Server Database.in this Database, there is a table tbl_product with columns P_ID,title,count,price and active. I don't know, how do it. When I run python api.py on http://localhost:5000/add, I see { "message": "The method is not allowed for the requested URL."}. Also, When I run python app.py, on http://localhost:8080/addI see Method Not Allowed. The method is not allowed for the requested URL . Can you help me?

my app.py file is:

 from flask import Flask,render_template,url_for, request, redirect, flash, jsonify,json
 import pyodbc
 import requests
 from api import ProductAdd

 app = Flask(__name__)
 app.secret_key= "flash_message"


 # creating connection Object which will contain SQL Server Connection
 conn = pyodbc.connect('Driver={SQL Server};'
                  'Server=TABRIZIYAN;'
                  'Database=market_DB;'
                  'Trusted_Connection=yes;')
 cursor = conn.cursor() 

@app.route('/add')
def add(): 
    if request.method =='POST':
        productDetails= request.form
        title= productDetails['title']
        count=productDetails['count']
        price= productDetails['price']
        active= productDetails['active']
        create_row_data = { 'title':str(title), 'count':str(count), 
                            'price':str(price), 'active':str(active) }
        info = requests.post('http://localhost:5000/add', data= create_row_data) 
        return info.text   #return(render_template('product.html'))
    else:
        return (render_template('add.html'))

if __name__=='__main__':
    app.run(debug=True, port="8080")

my api.py file is:

from flask import Flask, request, render_template,url_for, redirect, flash
from flask_restplus import Api, Resource
from flask import jsonify
import pyodbc
import requests

 flask_app = Flask(__name__)
 api = Api(app=flask_app)

 class ProductAdd(Resource):
     def post(self): 
          productDetails= request.json
          title= productDetails['title']
          count=productDetails['count']
          price= productDetails['price']
          active= productDetails['active']
          cursor = conn.cursor()
          cursor.execute(" INSERT INTO Tbl_product(title, count, price, active) VALUES(?, ?, ?, ?) 
                         ",(title,count,price,active))
          conn.commit()
          resp = jsonify('User added successfully!')
          resp.status_code = 200
          return resp

api.add_resource(ProductAdd , '/add')

if __name__ == '__main__':
    flask_app.run(debug=True)
mina
  • 153
  • 6
  • 18
  • 1
    Please clarify what your actual issue is. "Am I doing it right?" is too vague for us to understand what exactly you need help with – roganjosh Dec 28 '19 at 10:23
  • 2
    change ```@app.route('/add')``` to ```@app.route('/add', methods=['GET', 'POST']) – steve Dec 28 '19 at 18:30

0 Answers0