OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

FastAPI handling and redirecting 404

  • Thread starter Thread starter Yagiz Degirmenci
  • Start date Start date
Y

Yagiz Degirmenci

Guest
How can i redirect a request with FastAPI if there is a HTTPException?

In Flask we can achieve that like this:

Code:
@app.errorhandler(404)
def handle_404(e):
    if request.path.startswith('/api'):
        return render_template('my_api_404.html'), 404
    else:
        return redirect(url_for('index'))

Or in Django we can use django.shortcuts:

Code:
from django.shortcuts import redirect

def view_404(request, exception=None):
    return redirect('/')

How we can achieve that with FastAPI?
<p>How can i redirect a request with FastAPI if there is a HTTPException?</p>
<p>In Flask we can achieve that like this:</p>
<pre><code>@app.errorhandler(404)
def handle_404(e):
if request.path.startswith('/api'):
return render_template('my_api_404.html'), 404
else:
return redirect(url_for('index'))
</code></pre>
<p>Or in Django we can use django.shortcuts:</p>
<pre><code>from django.shortcuts import redirect

def view_404(request, exception=None):
return redirect('/')
</code></pre>
<p>How we can achieve that with FastAPI?</p>
 

Latest posts

А
Replies
0
Views
1
Али-Мухаммад Закарьяев
А
M
Replies
0
Views
1
Marcos R. Guevara
M
M
Replies
0
Views
1
Marcos R. Guevara
M
Top