Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Temporarily redirect all URLs to one view

I am building site and thought it would be nice to have some sort of maintenance page where I could redirect users if need be.

How can I start redirecting all requests to one specific view? I am using Constance to have maintenance switch in my admin view (just a bool value). Its value is then available thorough the project.

I've already prepared another list of urlpatterns but cannot figure out how to dynamically change them so the redirect works.

maintenance_urlpatterns = [
    url(r'^$', views.maintenance, name='maintenance'),
]

This is urls.py file from app, not the project one which I would leave alone.

I also thought about modifying base.html template and rendering "maintenance page" this way, but I think that is pretty bad solution.

EDIT: To clarify. My main question isnt how to write urlpattern that will capture all traffic, but how to edit these dynamically in response to Constance config change. So I can toggle that in admin.

like image 425
Filip Avatar asked May 21 '17 22:05

Filip


1 Answers

The regex in your question will only match on / (the root of your site). If you want to catch everything, just use ^ only. Every URL has a beginning, so ^ always matches.

url(r'^', views.maintenance, name='maintenance')

Of course, this depends upon your normal urlpatterns being disabled in some fashion. It sounds like maybe you have that covered already.

like image 197
Dan Lowe Avatar answered Sep 22 '22 07:09

Dan Lowe