61. Flask‐Admin, Error Pages - MantsSk/CA_PTUA14 GitHub Wiki

Flask-Admin, Error Pages

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

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.

Installation

pip install flask-admin

Integrating Flask-Admin into Your Application

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

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.

Error pages

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.

Creating Custom Error Pages

@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: image

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 %}
⚠️ **GitHub.com Fallback** ⚠️