Reputation: 372
last year i developed a big website using Flask.
I used render_template method to send data from back-end to front-end and request.form to get data from front-end to back-end
Now, my product owner wants me to use "API" instead of above solutions. And i'm fully confused how to use API.
Should i remove any render_template or request.form?
Should i change my back-end and view functions completely?
Am i able to just change render_template and request.form and make no change in the remaining code ?
Can i use Jinja template after using API? or i should use javascript?
my previous solution was like this:
apps = apps_model.query.all ()
render_template ('index.html' , apps=apps)
and:
user_name = request.form ['username']
Upvotes: 2
Views: 3727
Reputation: 1076
my product owner wants me to use "API" instead of above solutions
My understanding of this means that your product manager wants you to seperate the backend from the frontend. Simply put, you should create an [RESTful] API. In essence, you don't have to use render_template
to process and parse the HTML templates and display it to the end user.
Your new API should return JSON objects and then, from the client side (i.e. the website), the API gets called to create, update, retrieve and delete data from the database and then return the information to the client.
from flask import Flask, jsonify
app = Flask(__name__)
app.route("/", methods=["GET"])
def index():
api_response = {
"status": "success",
"message": "Welcome to our API"
}
return jsonify(api_response)
Should i remove any render_template or request.form?
You will not be needing render_template
anymore as you won't be displaying or returning HTML pages. However, request.form
can still be used to collect data from the client side. Totally, depends on how you want the client to communicate with your API
Should i change my back-end and view functions completely?
Only change/create the view functions to return specific data.
Sample cases:
Get all users: create route /api/users
Get user A: create route /api/users/user_a
Am i able to just change render_template and request.form and make no change in the remaining code ?
No.
Can i use Jinja template after using API? or i should use javascript?
Very unlikely. You won't be rendering template anymore. Should I use Javascript? Certainly, for your client side.
See https://www.restapitutorial.com
Upvotes: 7