How to reload urls without restarting the server

I want to implement a global lockdown scenario in the app after some event occurs and I am using an if condition in the core urls.py which reference to a global variable to implement control. Upon triggering of the global variable I want to redirect all urls to same endpoint /security ( for example ). But I came to know that the url conf is loaded just once at the starting of the server. How can I implement this?

( I am just a beginner and learning Django )

Hi. Using a global variable to control the flow in urls.py is not going to be the best approach.
Instead I recommend you to use some kind of Middleware for that.
Something Like this will work for your needs:

from django.http import HttpResponseRedirect
from django.urls import reverse

class GlobalLockdownMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # Check the value of your global variable or any other condition
        # you want to use to trigger the lockdown. For example, you could
        # use a setting or a cache value.
        lockdown_triggered = False

        if lockdown_triggered and request.path != reverse('security'):
            # Redirect all requests to the /security endpoint
            return HttpResponseRedirect(reverse('security'))

        response = self.get_response(request)
        return response
1 Like

or you can do this via nginx… (or apache or whatever)?

nginx.conf, site_enabled.conf, whatever:

server{
    ...
    error_page 503 /maintenance.html;
    location /maintenance.html{
        internal;
    }
    ...
    location / {
        include /etc/nginx/maintenance.conf;
        if ($maintenance = 1){
            return 503;
        }
    }
    ...
}

maintenance.conf:

set $maintenance 0;

or:

set $maintenance 1;

since this is all based on a single file, very easy to write, it can be very easy to script (python, bash, you name it).

While maintenance==1, restart your application to reload your new urls, something like:

reload_urls.sh:

# write maintenance=1;
# restart nginx
# restart django app
# write maintenance=0
# restart nginx
1 Like

Identify the web server software being used. Different web server have different method for loading URLs without retsarting the server.

Hello this is Gulshan Negi
Well, Middleware is one method for putting a global lockdown scenario into a Django app. Hooks for processing requests and responses as they pass through the Django app can be added using middleware.
If the lockdown scenario is in effect, you can develop a custom middleware class that checks the global variable and redirects all URLs to the /security endpoint. Here is an example:

class LockdownMiddleware:
def init(self, get_response):
self.get_response = get_response

def __call__(self, request):
    if lockdown_in_effect:
        return redirect('/security')
    response = self.get_response(request)
    return response

Thanks