Django configuration

Django

To run Django on the Engineering web server, use the following hints. In the hints, replace the name samplewebsite with the name of your web site.

Starting a project

To start a new project, go to the top directory for the web site:

cd /web/groups/samplewebsite/public_html

Create a project:

/opt/python/current/bin/django-admin startproject myproject .

Edit the settings:

edit myproject/settings.py

Change the allow hosts to any host:

ALLOWED_HOSTS = ['*']

Set the static directory's root directory:

STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

Change the static URL to the web site's name:

STATIC_URL = '/samplewebsite/static/'

Initialize the project:

./manage.py makemigrations
./manage.py migrate

Create a superuser account:

./manage.py createsuperuser

Create a static directory:

./manage.py collectstatic

.htaccess

Create an .htaccess file to direct the web site to the CGI script:

RewriteEngine On
RewriteBase /samplewebsite/
RewriteRule ^db\.sqlite3 - [F]
RewriteRule ^manage\.py - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ django.cgi/$1 [QSA,L]

CGI script

Create a CGI script named "django.cgi". Be sure to substitute the name of the web site:

#!/usr/local/bin/python
import os, sys
WWW_PATH = os.path.dirname(os.path.abspath(__file__))
DJANGO_PROJECT_PATH = os.path.dirname(WWW_PATH)
sys.path.append(DJANGO_PROJECT_PATH + "/")
sys.path.append(DJANGO_PROJECT_PATH + "/samplewebsite")
def run_with_cgi(application):
    environ                      = dict(os.environ.items())
    environ['wsgi.input']        = sys.stdin
    environ['wsgi.errors']       = sys.stderr
    environ['wsgi.version']      = (1,0)
    environ['wsgi.multithread']  = False
    environ['wsgi.multiprocess'] = True
    environ['wsgi.run_once']     = True
    if environ.get('HTTPS','off') in ('on','1'):
        environ['wsgi.url_scheme'] = 'https'
    else:
        environ['wsgi.url_scheme'] = 'http'
    headers_set  = []
    headers_sent = []
    def write(data):
        if not headers_set:
             raise AssertionError("write() before start_response()")
        elif not headers_sent:
             status, response_headers = headers_sent[:] = headers_set
             sys.stdout.write('Status: %s\r\n' % status)
             for header in response_headers:
                 sys.stdout.write('%s: %s\r\n' % header)
             sys.stdout.write('\r\n')
        
        sys.stdout.write(data)
        sys.stdout.flush()
    def start_response(status,response_headers,exc_info=None):
        if exc_info:
            try:
                if headers_sent:
                    raise exc_info[0], exc_info[1], exc_info[2]
            finally:
                exc_info = None
        elif headers_set:
            raise AssertionError("Headers already set!")
        headers_set[:] = [status,response_headers]
        return write
    result = application(environ, start_response)
    try:
        for data in result:
            if data:
                write(data)
        if not headers_sent:
            write('')
    finally:
        if hasattr(result,'close'):
            result.close()
try:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "samplewebsite.settings")
    import django.core.wsgi
    run_with_cgi(django.core.wsgi.get_wsgi_application())
except Exception, inst:
    print("Content-type: text/html\n\n")
    print(inst)

Be sure to set the script "django.cgi" to executable with:

chmod +x django.cgi

Last modified: 2020/06/24 12:43:15.749598 GMT-4 by curtis.f.smith.1
Created: 2018/10/10 13:18:25.284592 GMT-4 by curtis.f.smith.1.

Categories

Search the Knowledge Base

Include:

Quick search results

    Admin Options: Edit this Document