INSTALLED_APPS = [ # một số app khác, 'maintenance_mode', ]
3. Next, add middleware for django-maintenance-mode to MIDDLEWARE in settings.py file :
MIDDLEWARE = [ # Một số middleware khác của django, 'maintenance_mode.middleware.MaintenanceModeMiddleware', ]
For the django-maintenance-mode package that shows a 503 error page, it looks for a 503.html template file in the templates directory . To set it up, do the following:
1. Create a folder named templates in the root folder.
2. Open the newly created templates folder and create a file named 503.html .
3. In the settings file, select the TEMPLATES setting and configure the DIRS list within it as follows:
'DIRS': [BASE_DIR/'templates'],
4. Open the file 503.html and write the HMTL code to display an error message to the user. Here is sample code that you can use:
503 Service Unavailable
Oops! We are currently working on some updates. We apologize for the inconvenience and appreciate your patience.
Please visit the website later or contact our support team
Contact support
In the settings.py file, add this code to enable maintenance mode:
MAINTENANCE_MODE = True
Restart the development server by running the following command into the CLI:
python manage.py runserver
When navigating to the web, you will see the created maintenance page.
To allow the admin site to continue working even when in maintenance mode, django-maintenance-mode provides a setting named MAINTENANCE_MODE_IGNORE_ADMIN_SITE . You would add this setting to the settings.py file and set it to True :
MAINTENANCE_MODE_IGNORE_ADMIN_SITE = True
The default value of the above setting is False , so the admin page here will be affected by maintenance mode if you don't set it to True .
The django-maintenence-mode package provides a decorator to prevent a window or certain page, such as About - from going into maintenance mode. To do this, first import the decorator into the views.py module .
from maintenance_mode.decorators import force_maintenance_mode_off
After importing decorator, add it to the viewer like so:
@force_maintenance_mode_off def view_name(request): # thực hiện logic cửa sổ xem # không bao giờ quay lại phản hồi 503 response
After implementing the appropriate decorator, the URL of the particular view window will allow the user to access it.
Ignoring a class-based viewer is similar to omitting a function-based viewer. However, the best method here is to do it in the urls.py file .
First, you need to enter decoratorforce_maintenance_mode_off in your app's urls.py file. Then you need to include it in the URL path. For example:
from maintenance_mode.decorators import force_maintenance_mode_off from .views import YourView urlpatterns = [ # không bao giờ quay lại phản hồi 503 path('', force_maintenance_mode_off(YourView.as_view()), name='my_view'), ]
Make sure you also enter the necessary data such as the path and the class-based viewer.
1. To enable maintenance mode for a single view window, first disable maintenance mode in the settings.py file as follows:
MAINTENANCE_MODE = False
2. Next, in views.py , you should enter decorator force_maintenance_mode_on and add it to the view window:
from maintenance_mode.decorators import force_maintenance_mode_on @force_maintenance_mode_on def view_name(request): # Perform view logic # Always return 503 response
First, you need to disable maintenance mode in the settings.py file :
MAINTENANCE_MODE = False
Next, in urls.py , you should import decorator and add it to the requested URL path:
from maintenance_mode.decorators import force_maintenance_mode_on from .views import YourView urlpatterns = [ # Always return 503 response path('', force_maintenance_mode_on(YourView.as_view()), name='my_view'), ]
By default, the django-maintenance-mode package looks for templates/503.html . You can decide to override it in the settings.py file .
Assuming you have a separate folder for error handling in your app, you'll want to include the 503.html template in this folder. Therefore, your template will be in templates/errors/503.html .
The default settings for this configuration are:
MAINTENANCE_MODE_TEMPLATE = "503.html"
To override it, you should add another path that points to the error page. For example:
MAINTENANCE_MODE_TEMPLATE = "errors/503.html"
You can also change the filename if you want, and everything will be fine if you add the necessary configurations.
Upgrading the maintenance mode in the app can make things easier for you and your users. By temporarily disabling access to all or part of your app during updates or maintenance, you can minimize interruptions and errors that arise from concurrent user interactions.