Sign up to create your own snipts, or login.

Public snipts » Fotinakis's snipts » django The latest django snipts from Fotinakis.

showing 1-5 of 5 snipts for django
  • Dynamic Django mod_wsgi hook files
    # --- django.wsgi - in any project
    from some_common_module import wsgi_helper
    application = wsgi_helper.setup(__file__)
    
    
    # --- wsgi_helper.py - in some_common_module
    import os, sys
    import django.core.handlers.wsgi
    
    def setup(filename):
    	project_root = os.sep.join(filename.split(os.sep)[:-2])
    	project_dir = None
    	
    	for path in os.listdir(project_root):
    		# Find each dir off the root, and check it for settings.py
    	
    		path = os.path.join(project_root, path)
    	
    		if os.path.isdir(path):
    			file = "".join([path, os.sep, "settings.py"])
    			if os.path.exists(file):
    				# We have found /project_name/settings.py
    				project_dir = path
    				os.environ['DJANGO_SETTINGS_MODULE'] = '%s.settings' % path.split(os.sep)[-1]
    	
    	if not project_dir:
    		raise Exception("Could not find project settings file! Checked path: %s" % path)
    	
    	if project_root not in sys.path: sys.path.insert(0, project_root)
    	if project_dir not in sys.path: sys.path.insert(0, project_dir)
    
    	return django.core.handlers.wsgi.WSGIHandler()
    

    copy | embed

    0 comments - tagged in  posted by Fotinakis on Feb 26, 2010 at 6:39 p.m. EST
  • Dynamic directories in Django settings.py
    # In settings.py
    PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
    ROOT_DIR = os.path.split(PROJECT_DIR)[0]
    
    
    MEDIA_ROOT = ROOT_DIR + '/static/'
    MEDIA_URL = '/static/'
    
    TEMPLATE_DIRS = (
    	# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    	# Always use forward slashes, even on Windows.
    	# Don't forget to use absolute paths, not relative paths.
    	PROJECT_DIR + '/templates/',
    )
    

    copy | embed

    0 comments - tagged in  posted by Fotinakis on Jun 29, 2009 at 5:51 p.m. EDT
  • Django template tag for dynamic attribute lookups
    import re
    from django import template
    from django.conf import settings
    
    numeric_test = re.compile("^\d+$")
    register = template.Library()
    
    def getattribute(value, arg):
    	"""Gets an attribute of an object dynamically from a string name"""
    	
    	if hasattr(value, str(arg)):
    		return getattr(value, arg)
    	elif hasattr(value, 'has_key') and value.has_key(arg):
    		return value[arg]
    	elif numeric_test.match(str(arg)) and len(value) > int(arg):
    		return value[int(arg)]
    	else:
    		return settings.TEMPLATE_STRING_IF_INVALID
    
    register.filter('getattribute', getattribute)
    
    # Then, in template:
    # {% load getattribute %}
    # {{ object|getattribute:dynamic_string_var }}
    

    copy | embed

    0 comments - tagged in  posted by Fotinakis on May 11, 2009 at 6:32 p.m. EDT
  • Django mod-wsgi hook file
    import os, sys
    import django.core.handlers.wsgi
    
    path = '/var/www/sub.example.com'
    if path not in sys.path: sys.path.append(path)
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'project_name.settings'
    
    application = django.core.handlers.wsgi.WSGIHandler()
    

    copy | embed

    0 comments - tagged in  posted by Fotinakis on Apr 22, 2009 at 3:36 p.m. EDT
  • Mass dynamic Django hosting based on hostname
    <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/root
    
            RewriteEngine On
            RewriteMap tolower int:tolower
    
            # Redirect all www.
            RewriteCond   %{HTTP_HOST}                 ^www\..*$
            RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
            RewriteRule   ^www\.(.*) http://$1 [R=301,L]
    
            # For all admin media
            RewriteRule ^/media(.*) /usr/share/python-support/python-django/django/contrib/admin/media$1 [L]
    
            # For each project's static files
            RewriteRule ^/static(.*) /var/www/${tolower:%{SERVER_NAME}}/static$1
            RewriteRule ^/favicon.ico /var/www/${tolower:%{SERVER_NAME}}/static/images/favicon.ico
    
            # For each project's documentation
            RewriteRule ^/docs(.*) /var/www/${tolower:%{SERVER_NAME}}/docs/build/html$1
    
            # For each project's WSGI hook
            RewriteRule ^/(?!var/www/)(.*) /var/www/${tolower:%{SERVER_NAME}}/apache/django.wsgi/$1
            RewriteRule . - [E=APPLICATION_GROUP:${tolower:%{SERVER_NAME}}]
    
            WSGIProcessGroup %{GLOBAL}
            WSGIApplicationGroup %{ENV:APPLICATION_GROUP}
    
            # Run mod_wsgi in daemon mode, with daemons named by the site name 
            WSGIDaemonProcess %{ENV:APPLICATION_GROUP} processes=5 threads=10 python-path=/path/to/common display-name=%{GROUP}
    
            <Directory /var/www/*/apache>
                    Order allow,deny
                    Allow from all
                    Options ExecCGI
                    AddHandler wsgi-script .wsgi
            </Directory>
    
            <Directory /var/www/*/docs>
                    # You can protect the docs directories here
            </Directory>
    
            ErrorLog /var/log/apache2/error.log
    
            # Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
            LogLevel warn
    
            CustomLog /var/log/apache2/access.log combined
    </VirtualHost>
    

    copy | embed

    0 comments - tagged in  posted by Fotinakis on Apr 22, 2009 at 3:31 p.m. EDT
Sign up to create your own snipts, or login.