61. Flask‐Admin, Error Pages - MantsSk/CA_PTUA14 GitHub Wiki
To follow this lesson either create a new Flask application with some database entries or continue with application we were building in previous lessons - https://github.com/MantsSk/CA_PTUA10/tree/master/60Pamoka%20-%20FlaskRelationships/end_code
Flask-Admin is a Flask extension that provides an administrative interface for your Flask application. It allows you to quickly create CRUD (Create, Read, Update, Delete) interfaces for your data models, making it easier to manage your application's data.
pip install flask-admin
Let's integrate Flask-Admin into our existing Flask application step by step:
Import Necessary Modules: First, we need to import the necessary modules from Flask-Admin and SQLAlchemy:
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
Initialize Flask-Admin with your Flask application:
admin = Admin(app)
Next, create a view for the User model using Flask-Admin's ModelView:
admin.add_view(ModelView(User, db.session))
Finally, run your Flask application as usual. You should now be able to access the Flask-Admin interface at /admin.
![image](https://private-user-images.githubusercontent.com/22573801/327394218-0eb0f397-4a28-4b5a-992c-1cef8b016ef0.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUxNDk5NDIsIm5iZiI6MTcyNTE0OTY0MiwicGF0aCI6Ii8yMjU3MzgwMS8zMjczOTQyMTgtMGViMGYzOTctNGEyOC00YjVhLTk5MmMtMWNlZjhiMDE2ZWYwLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MDElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTAxVDAwMTQwMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTlmMWM4YTVhOGI2NGViYzhkZWUwZjIyYmQ1NTQyNGU1MDcwODJjZDhlY2MwNWQyMTRlZTZhOWU2OTZjN2Y0MjAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.nxz6jhy1EJlyEEIK828KGw2dv5keMC0-OHZiXP5-Ark)
One additional thing that we should do, is to limit who can access this admin page. Now everyone has access, which is not ideal. Let's make sure that only admin account can access this. We can do that by inheriting from ModelView and changing is_accessible method:
class AdminModelView(ModelView):
def is_accessible(self):
return current_user.is_authenticated and current_user.username == "admin"
admin.add_view(AdminModelView(User, db.session))
admin.add_view(AdminModelView(Post, db.session))
Now, only authenticated user with username "admin" will be able to access the admin panel.
Before we dive into creating custom error pages, let's understand the basic error handling mechanism in Flask. Flask provides decorators to handle specific error codes, for example:
@app.errorhandler(404): Handles 404 Not Found errors.
@app.errorhandler(500): Handles 500 Internal Server errors.
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404
Here, 404.html is a custom HTML template for the 404 error page. You can add it to your templates folder. The template can look something like this:
{% extends "base.html" %}
{% block title %}404 Not Found{% endblock %}
{% block content %}
<h1>404 Not Found</h1>
<p>The requested URL was not found on the server.</p>
<p>Please check the URL or go back to the <a href="{{ url_for('home') }}">home page</a>.</p>
{% endblock %}
Now, when you will visit non-existent page on website, you will get your custom error page:
You can follow the same process for other errors, example:
@app.errorhandler(500)
def internal_server_error(error):
return render_template('500.html'), 500
500.html:
{% extends "base.html" %}
{% block title %}500 Internal Server Error{% endblock %}
{% block content %}
<h1>500 Internal Server Error</h1>
<p>Sorry, something went wrong on the server.</p>
<p>Please try again later or contact the administrator.</p>
{% endblock %}