Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Commit 25e9619

Browse files
Sridhar RatnakumarPatrick Bozeman
authored andcommitted
Document use of VCAP_SERVICES in django's settings.py
(also removed the sample app, as it lives in the tests repo) Change-Id: I5cdf9995584f20c4df38877840c400899e86a451
1 parent 28a6a46 commit 25e9619

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

docs/python.md

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,41 @@ A special framework called "Django" exists to also perform Django-specific
2323
staging actions. At the moment, this runs `syncdb` non-interactively to
2424
initialize the database.
2525

26+
### Accessing the database
27+
28+
Cloud Foundry makes the service connection credentials available as JSON via the
29+
`VCAP_SERVICES` environment variable. Using this knowledge, you can use the
30+
following snippet in your own settings.py:
31+
32+
## Pull in CloudFoundry's production settings
33+
if 'VCAP_SERVICES' in os.environ:
34+
import json
35+
vcap_services = json.loads(os.environ['VCAP_SERVICES'])
36+
# XXX: avoid hardcoding here
37+
mysql_srv = vcap_services['mysql-5.1'][0]
38+
cred = mysql_srv['credentials']
39+
DATABASES = {
40+
'default': {
41+
'ENGINE': 'django.db.backends.mysql',
42+
'NAME': cred['name'],
43+
'USER': cred['user'],
44+
'PASSWORD': cred['password'],
45+
'HOST': cred['hostname'],
46+
'PORT': cred['port'],
47+
}
48+
}
49+
else:
50+
DATABASES = {
51+
"default": {
52+
"ENGINE": "django.db.backends.sqlite3",
53+
"NAME": "dev.db",
54+
"USER": "",
55+
"PASSWORD": "",
56+
"HOST": "",
57+
"PORT": "",
58+
}
59+
}
60+
2661
### Limitations
2762

2863
* Django admin (if your app uses it) will be unusable as superusers are not
@@ -31,28 +66,3 @@ initialize the database.
3166
* Migration workflow, such as that of [South](http://south.aeracode.org/) are
3267
not supported.
3368

34-
## Sample applications
35-
36-
### A hello world WSGI application
37-
38-
Here's a sample WSGI application using the "bottle" web framework.
39-
40-
$ mkdir myapp && cd myapp
41-
$ cat > wsgi.py
42-
import os
43-
import sys
44-
import bottle
45-
46-
@bottle.route('/')
47-
def index():
48-
pyver = '.'.join(map(str, tuple(sys.version_info)[:3]))
49-
return 'Hello World! (from <b>Python %s</b>)' % (pyver,)
50-
51-
application = bottle.default_app()
52-
53-
if __name__ == '__main__':
54-
bottle.run(host='localhost', port=8000)
55-
56-
$ cat > requirements.txt
57-
bottle
58-
$

0 commit comments

Comments
 (0)