Skip to content

Instantly share code, notes, and snippets.

@esseti
Created January 29, 2013 12:07
Show Gist options
  • Save esseti/4663776 to your computer and use it in GitHub Desktop.
Save esseti/4663776 to your computer and use it in GitHub Desktop.
This is an example of how to write your setting.py (Django) for running the app on AppFog and localhost without the need of changing settings every time. Plus at the bottom i added a trick on how to init the db, since AppFog does not let to do that (you have to use the tunnelling)
# Django settings for DjangoProjects project.
import os
#if this value exsist you are on appfog.
if 'VCAP_SERVICES' in os.environ:
DEBUG = False
#othewhise it's localhost
else:
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
...
)
MANAGERS = ADMINS
if DEBUG:
DATABASES = {
# here goes the db setting for your localhost
}
}
else:
# this is the setting for MYSQL-5.1 on appfog. it's standard, so nothing to change
import json
vcap_services = json.loads(os.environ['VCAP_SERVICES'])
mysql_srv = vcap_services['mysql-5.1'][0]
cred = mysql_srv['credentials']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': cred['name'],
'USER': cred['user'],
'PASSWORD': cred['password'],
'HOST': cred['hostname'],
'PORT': cred['port'],
}
}
...
#this set the statuc url
if DEBUG:
STATIC_URL = '/static/'
# if on appfog, static files MUST be hosted in another server. put the link here
else:
STATIC_URL = 'http://..'
....
#template setting, local and remote (i've templates inside the foldere general/template/)
if DEBUG:
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, "../general/template"),
)
else:
TEMPLATE_DIRS = (
LOCATION+'general/template',
)
...
#debug setting. on APPFOG i set them to warning. localhost to DEBUG
if DEBUG:
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format' : "[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)s] %(message)s",
'datefmt' : "%d/%b/%Y %H:%M:%S"
},
},
'handlers': {
'null': {
'level':'DEBUG',
'class':'django.utils.log.NullHandler',
},
'console':{
'class':'logging.StreamHandler',
'formatter': 'standard'
},
},
'loggers': {
'django': {
'handlers':['console'],
'propagate': True,
'level':'WARN',
},
'django.db.backends': {
'handlers': ['console'],
'level': 'WARN',
'propagate': False,
},
'crowdmachine': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},'executor': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},'general': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},'requester': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},
'SocialAuth': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},
'restapi': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},
}
}
else:
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format' : "[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)s] %(message)s",
'datefmt' : "%d/%b/%Y %H:%M:%S"
},
},
'handlers': {
'null': {
'level':'INFO',
'class':'django.utils.log.NullHandler',
},
'console':{
'class':'logging.StreamHandler',
'formatter': 'standard'
},
},
'loggers': {
'django': {
'handlers':['console'],
'propagate': True,
'level':'INFO',
},
'django.db.backends': {
'handlers': ['console'],
'level': 'INFO',
'propagate': False,
},
'crowdmachine': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
},'executor': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
},'general': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
},'requester': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
},
'SocialAuth': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
},'restapi': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
},
}
}
#function to init the db (create admin)
#be sure to call this after the db is created.
from django.contrib.auth.models import User
from docs import settings
from rest_framework.authtoken.models import Token
def createAdmin(username,password,email):
admin, c = User.objects.get_or_create(username=username)
if c:
admin.set_password(password)
admin.email=email
admin.is_superuser = True
admin.is_staff = True
admin.save()
print 'new'
else:
admin.set_password(password)
admin.save()
print 'update'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment