Flask How to Create Routes with Flask-Classy

In this Flask article we are going to learn How to Create Routes with Flask-Classy, Flask-Classy is an extension that adds class-based views to Flask. so Flask-Classy will automatically generate routes based on the methods in your views, and makes it super simple to override those routes using Flask’s familiar decorator syntax. if your familiar with Flask, iam sure you have used the default routing in Flask.  in that way you can create routes using decorators.

 

 

Also you can read more articles on Flask.

1: Flask Building REST API with Marshmallow

2: Flask CRUD Application with SQLAlchemy

3: Flask Create News Web Application

 

 

 

So first of all we need to install Flask-Classy, because it is a third party library, and you can install that using pip command like this.

 

 

 

 

OK first let’s just create routes using the default way, and after that we are going to use Flask-Classy for creating of routes.

 

So it is just a simple flask app with one url route.

 

 

 

if you run this and go to http://localhost:5000/home, this will be the result.

 

Flask Default Route
Flask Default Route

 

 

 

 

OK now let’s create our route using Flask-Classy.

 

You can see that in the above code, we have just created a Class, and my class extends from FlaskView.

 

 

 

If you run the code and go to http://localhost:5000/home/, this will be the result.

 

Flask-Classy Example
Flask-Classy Example

 

 

 

 

OK, now let’s create another example, and in this example the routes creation will be automatically.

 

 

 

 

If you run the code and go to http://localhost:5000/home/5, this will be the result.

Flask Classy Example
Flask Classy Example

 

 

You can see that Flask-Classy will automatically create routes for any method in a FlaskView that doesn’t begin with an underscore character. You can still define your own routes of course.

 

 

 

 

Using Custom Routes 

So now let’s just use custom routes with Flask-Classy.

 

 

 

 

 

If you run the code and go to http://localhost:5000/home/contact/, this will be the result.

 

Flask-Classy Custom Routes
Flask-Classy Custom Routes

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×