Forums

How to temporarily take my django site down

I have a major update to make to my django site, including some large data migrations that will take some time. My site has enough traffic that this will certainly impact at lest a few users. I'd like to temporarily put up an "under construction" static page while I complete the update. What's the best approach to do this on pythonanywhere?

On a "normal" server, I would edit my .htaccess file to do a 302 redirect, but I don't believe that's an option here.

I think the easiest way will be to modify your WSGI file (which you can get to from the link on the the "Web" tab).

Right now, you'll have code to load up Django, which will look something like this:

import os
import sys

# assuming your django settings file is at '/home/giles/mysite/mysite/settings.py'
# and your manage.py is is at '/home/giles/mysite/manage.py'
path = '/home/giles/mysite'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

# then:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

What you'll need to do is comment it out, and then add code like this underneath -- it's raw WSGI code that will just respond to every request with a "site down for maintenance" response:

def application(environ, start_response):
    status = '200 OK'
    content = "Site down for maintenance"
    response_headers = [('Content-Type', 'text/html'), ('Content-Length', str(len(content)))]
    start_response(status, response_headers)
    yield content.encode('utf8')

Once you're done that, reload the site from the "Web" page and check it to make sure that it's working the way you want. You can modify what it displays by changing the content string -- just put any HTML you like in there.

To get back to showing your site when you're done, just delete (or comment out) the raw WSGI code, and uncomment your Django code.