Flask-Back is a lightweight Flask extension for managing "back" URLs to help users return to their previous page in a clean and configurable way.
- Automatically or manually save back URLs
- Use
{{ back_url }}
in templates - Fallback to the referrer if no back URL is saved
- Exclude specific endpoints from tracking
- Session-based and lightweight
pip install flask-back
from flask import Flask, redirect
from flask_back import Back
app = Flask(__name__)
app.secret_key = "supersecret"
back = Back(app, default_url="/", use_referrer=True)
@app.route("/save")
@back.save_url
def save_page():
return "This page is now saved as the back URL."
@app.route("/go-back")
def go_back():
return redirect(back.get_url())
@app.route("/excluded")
@back.exclude
def excluded_page():
return "This page won't be tracked as a back URL."
In templates:
<a href="{{ back_url }}">Go Back</a>
You can pass these options when initializing:
Back(app,
default_url="/fallback", # Where to go if nothing is saved
use_referrer=True, # Use Referer header as fallback
excluded_endpoints=["static"] # List of endpoints to skip
)
Back(app=None, **settings)
– Create the extensionsave_url(func)
– Decorator to manually mark routes as back URLsget_url(default=None)
– Retrieve saved URL or fallbackclear()
– Clear current back URL from sessionexclude(func)
– Decorator to ignore tracking for a route
pytest
pytest --cov=src --cov-report=term-missing
See CONTRIBUTING.md for setup, testing, and PR tips.
Found a vulnerability? Please report it via GitHub Issues or email Hcha.Byte@gmail.com.
This project is licensed under the MIT License. See LICENSE.
Flask-Back was built to solve a common pattern in web apps: helping users return to the right place—without relying solely on browser behavior. Now you can manage that flow clearly and securely.
⭐ Star the repo if you find it helpful!