Quickstarters
CRUD Samples
How to Develop a CRUD Application with Flask?
32 min
overview this tutorial will guide you through building a crud (create, read, update, delete) application using flask we'll integrate back4app as our backend service to simplify data management you will learn to set up a back4app project, design a flexible data structure, and implement crud functionalities using flask in this walkthrough, you'll first create a back4app project named basic crud app flask that offers a robust, non relational data storage solution you'll define your data schema with classes and fields either manually or using back4app’s ai agent after configuring your backend with back4app’s admin interface, you will connect your flask application to the service via rest api calls this tutorial also covers secure access management for your data by the end, you will have a production ready flask application capable of performing crud operations along with secure user authentication and data handling what you will learn how to create a flask based crud app with a non relational backend how to structure your backend on back4app and integrate it with a flask project how to utilize the back4app admin interface to manage crud operations effortlessly how to deploy your flask application, including using docker for containerization prerequisites before you begin, ensure you have a back4app account with a configured project need help? refer to getting started with back4app https //www back4app com/docs/get started/new parse app a python development environment set up use an editor like vscode or pycharm and install python 3 8 (or later) basic knowledge of python, flask, and rest apis consult the python documentation https //docs python org/3/ if necessary step 1 – initial project setup creating your back4app project log in to your back4app account click on “new app” from your dashboard enter the project name basic crud app flask and complete the setup process create new project after creation, your project appears on the dashboard, serving as the base for your backend configuration step 2 – building the data schema defining your data structures for this application, you'll establish several classes in your back4app project below are examples of the core classes and their fields essential for crud operations 1\ items class field type description id objectid auto generated unique identifier title string the name or title of the item description string a brief overview of the item createdat date timestamp when the item was created updatedat date timestamp for the latest update 2\ users class field type description id objectid auto generated unique identifier username string unique username for the user email string unique email address passwordhash string securely hashed password for authentication createdat date timestamp when the user account was created updatedat date timestamp for the last account update you can create these classes and add fields directly in your back4app dashboard create new class you can add columns by choosing a data type, naming the field, setting default values, and marking it as required create column utilizing back4app’s ai agent for schema creation the ai agent in your dashboard can automatically establish the schema based on your instructions this tool simplifies the process and ensures your data model is optimal for crud tasks how to use the ai agent open the ai agent sign in to your back4app dashboard and locate the ai agent under project settings describe your schema provide a detailed prompt listing the classes and fields confirm the setup review the proposed schema and approve the changes example prompt create the following classes in my back4app project 1\) class items \ fields \ id objectid (auto generated) \ title string \ description string \ createdat date (auto generated) \ updatedat date (auto updated) 2\) class users \ fields \ id objectid (auto generated) \ username string (unique) \ email string (unique) \ passwordhash string \ createdat date (auto generated) \ updatedat date (auto updated) this ai driven process saves time and guarantees a consistent, effective data model step 3 – enabling the admin interface & handling crud operations overview of the admin interface back4app’s admin interface provides a no code solution to manage your backend data with its drag and drop features, you can easily perform crud operations—such as adding, editing, or deleting records enabling the admin interface go to the “more” menu on your back4app dashboard select “admin app” and click “enable admin app ” create your admin account, which also sets up initial roles like b4aadminuser enable admin app after activation, sign in to the admin interface to manage your project data admin app dashboard using the admin interface for crud within the admin interface, you can insert new entries click “add record” within a class (e g , items) to create new data view and modify entries select any record to see details or update fields delete records remove entries that are no longer required this easy to use interface improves productivity by streamlining backend management step 4 – connecting your flask application to back4app with your backend set up, the next step is to link your flask application to back4app making api calls from flask since there isn’t a dedicated parse sdk for flask, you will use rest api calls in your flask app, you can utilize the requests library to perform crud operations example setting up api communication install the requests library run the following command in your terminal pip install requests configure a python module for api calls create a file called back4app api py # back4app api py import requests application id = "your application id" rest api key = "your rest api key" base url = "https //parseapi back4app com/classes" headers = { "x parse application id" application id, "x parse rest api key" rest api key, "content type" "application/json" } def get items() response = requests get(f"{base url}/items", headers=headers) return response json() def create item(title, description) data = {"title" title, "description" description} response = requests post(f"{base url}/items", headers=headers, json=data) return response json() def update item(object id, title, description) data = {"title" title, "description" description} response = requests put(f"{base url}/items/{object id}", headers=headers, json=data) return response json() def delete item(object id) response = requests delete(f"{base url}/items/{object id}", headers=headers) return response json() integrate api functions in your flask routes for example, in app py # app py from flask import flask, request, jsonify, render template from back4app api import get items, create item, update item, delete item app = flask( name ) @app route("/") def index() items = get items() return render template("index html", items=items get("results", \[])) @app route("/add", methods=\["post"]) def add item() title = request form get("title") description = request form get("description") result = create item(title, description) return jsonify(result) @app route("/update/\<item id>", methods=\["put"]) def modify item(item id) data = request get json() result = update item(item id, data get("title"), data get("description")) return jsonify(result) @app route("/delete/\<item id>", methods=\["delete"]) def remove item(item id) result = delete item(item id) return jsonify(result) if name == " main " app run(debug=true) this example illustrates how to use rest calls within flask to interact with your back4app backend step 5 – protecting your backend implementing access controls safeguard your data by configuring access controls for instance, when creating a new item, you might restrict access to its creator below is an example using api calls with controlled access import requests def create private item(title, description, user token) data = {"title" title, "description" description} headers = { "x parse application id" "your application id", "x parse rest api key" "your rest api key", "x parse session token" user token, "content type" "application/json" } response = requests post("https //parseapi back4app com/classes/items", headers=headers, json=data) return response json() configuring class level permissions adjust the class level permissions (clps) directly in the back4app dashboard to ensure only authenticated users can access specific classes step 6 – implementing user authentication setting up user management back4app offers built in user management via its users class in your flask app, you can create endpoints for registration and login example user registration and login \# auth py import requests application id = "your application id" rest api key = "your rest api key" headers = { "x parse application id" application id, "x parse rest api key" rest api key, "content type" "application/json" } def register user(username, password, email) data = {"username" username, "password" password, "email" email} response = requests post("https //parseapi back4app com/users", headers=headers, json=data) return response json() def login user(username, password) params = {"username" username, "password" password} response = requests get("https //parseapi back4app com/login", headers=headers, params=params) return response json() you can create corresponding flask routes to handle user registration and login using these functions step 7 – deploying your flask application back4app simplifies deployment you can deploy your flask app via several methods, including docker 7 1 packaging your flask application prepare your application ensure all required files and dependencies are included test locally run your app locally with flask run 7 2 organizing your project structure a typical structure might be basic crud app flask/ \| app py \| back4app api py \| auth py \| templates/ \| | index html \| static/ \| requirements txt \| dockerfile 7 3 containerizing with docker include a dockerfile at the project root \# use a lightweight python image from python 3 9 slim \# set the working directory workdir /app \# install dependencies copy requirements txt run pip install no cache dir r requirements txt \# copy the application code copy \# expose the port flask runs on expose 5000 \# start the flask app cmd \["python", "app py"] 7 4 deploying via back4app web deployment connect your github repository ensure your code is pushed to github configure deployment settings in the back4app dashboard, use the web deployment feature to link your repository (e g , basic crud app flask ) and select your branch define build commands specify the build command (e g , pip install r requirements txt ) and the location of your application deploy click deploy and monitor the logs until your application is live step 8 – wrapping up and future directions great job! you have successfully created a flask based crud application integrated with back4app you established a project named basic crud app flask , defined data models for items and users, and managed your backend via the back4app admin interface additionally, you connected your flask application using rest api calls and implemented security measures what’s next? expand functionality consider adding features like advanced search, detailed item views, or real time updates enhance backend services explore cloud functions, integrate third party apis, or implement role based access control deepen your skills visit the back4app documentation https //www back4app com/docs and other resources to further refine your application happy coding and best of luck with your flask crud application!