simple "helloworld" app:

# --- demo.py ---
"""
Self-contained demo script that bootstraps the helloworld
app and launch a WSGI server on localhost:8000. 

"""

import sys, os, logging
log = logging.getLogger('notmm.controllers.wsgi')

from notmm.utils.django_settings import SettingsProxy
from notmm.utils.configparse import has_conf, loadconf
from notmm.http import get_bind_addr, HTTPServer

class ConfigurationError(Exception):
    pass

if has_conf(os.getcwd(), 'development.ini'):
    app_conf = loadconf('development.ini')
    # Get the host/port using get_bind_addr.
    bind_addr = get_bind_addr(app_conf, 'httpserver')
else:
    raise ConfigurationError('development.ini not found!')

def main(app_label):
    """Launch a simple WSGI app in stand-alone mode. 
    
    """
    # Setup basic site environment
    #configure_minimal_env()
    sys.path.extend(['lib', 'lib/site-packages'])

    settings = SettingsProxy(os.environ['DJANGO_SETTINGS_MODULE'], \
        autoload=True).get_settings()
    if settings.DEBUG:
        log.debug("Found %d settings!"%settings.count())

    # setup default controller options
    from demo_app import WSGIApplication as WSGIApp
    wsgi_app = WSGIApp(settings)
 
    # Install some handlers; We can skip this part if the
    # controller instance has them configured already.
    #wsgi_app.sethandle('handle404', app_label + '.handlers.handle404')
    #wsgi_app.sethandle('handle500', app_label + '.handlers.handle500')
    WSGIServer = HTTPServer(wsgi_app, bind_addr)

    try:
        log.info("Starting HTTP server on %s:%d" % bind_addr)
        WSGIServer.serve()
    except (SystemExit, KeyboardInterrupt):
        log.info('Shutting down HTTP server...')
        sys.exit(2)

if __name__ == '__main__':
    main(app_label='helloworld')

simple "djangorc":

# source this file with bash for instant Django
# usage: source ./djangorc
# hack hack hack ...
ROOTDIR=`cd -P -- "$(dirname -- "$0")/" && pwd -P`
DJANGO_HOME=/path/to/django
PYTHONPATH="$ROOTDIR/lib:$ROOTDIR/lib/site-packages:$DJANGO_HOME"
DJANGO_SETTINGS_MODULE=local_settings
export ROOTDIR PYTHONPATH DJANGO_SETTINGS_MODULE DJANGO_HOME

local settings for Django :

# --- local_settings.py ---
from pkg_resources import resource_filename
from helloworld.config.settings import *

MEDIA_ROOT = resource_filename('helloworld', 'static')

DEBUG = False
USE_I18N = False
LANGUAGE_CODE = 'fr-CA'

DEFAULT_CHARSET = 'utf-8'
DEFAULT_CONTENT_TYPE = 'text/html; charset=%s' % DEFAULT_CHARSET

# Custom error handlers mapping
CUSTOM_ERROR_HANDLERS = (
    ('handle404', 'helloworld.handlers.handle404'),
    ('handle500', 'helloworld.handlers.handle500'),
    ('handle302', 'helloworld.handlers.handle302')
    )

# List of processors used by RequestContext to populate the context.
# Each one should be a callable that takes the request object as its
# only parameter and returns a dictionary to add to the context.
TEMPLATE_CONTEXT_PROCESSORS = ()

# For backward-compatibility with Django
TEMPLATE_DIRS = (
    (resource_filename('helloworld', 'templates')),
)

#DATABASE_ENGINE = 'sqlite3'
#DATABASE_NAME = 'sqlite:///fixtures/test.db'

ROOT_URLCONF = 'helloworld.config.urls'

# Logging options
LOGGING_FORMAT = '[%(levelname)-5s] - [%(asctime)-15s] - [%(name)-5s] - %(message)s' 
# Where to send application errors 
LOGGING_ERROR_LOG = '/var/log/python.log'

MIDDLEWARE_CLASSES = ()

helloworld.config.urls:

from notmm.utils.urlmap import RegexURLMap, url
from notmm.utils.django_settings import LazySettings

settings = LazySettings()
    
from notmm.release import VERSION
render_to_response = 'blogengine.template.direct_to_template'

extra_context = {
    #'settings': settings,
    'local_version': VERSION,
    'media_url': settings.MEDIA_URL 
}
urlpatterns = RegexURLMap()

urlpatterns.add_routes('',
   # custom image handler   
   url(r'(?P<path>\w+.(gif|png|jpg|ico))$', \
    'helloworld.views.image_handler', {
        'document_root' : settings.MEDIA_ROOT }
   )
)

urlpatterns.add_routes('',
   # Frontdoor (a simple generic view)
   url(r'^$|index.html$', render_to_response, {
        'template_name': 'default/index.mako',
        'extra_context': extra_context
   })
   )
#urlpatterns.commit()

EtienneRobillard/DjangoHotSauce/Examples (last edited 2017-01-23 16:59:26 by tkadm30)

Unable to edit the page? See the FrontPage for instructions.