Testing Django Projects at Scale
Talk given by Ash Christopher
Testing in Django
Published on Mar 12, 2012
Carl Meyer
A deep dive into writing tests with Django, covering Django’s custom test-suite-runner and the testing utilities in Django, what all they actually do, how you should and shouldn’t use them (and some you shouldn’t use at all!). Also, guidelines.
Splitting up the settings file
If you use a source control system (CVS, SVN, …), or want to publish your application on the web, it may be a good idea to move sensitive or machine/user specific settings like database passwords and such out of the main settings.py file.
As discussions on the ​django-developers mailing list have shown everybody has different requirements and ideas how to do this. This page is meant to collect some of these ideas for future reference.
One thing to keep in mind is that Django’s config files are pure Python. This gives you the ultimate flexibility to handle configurations the way you think is best. Or to quote Adrian Holovaty:
We don’t need a default solution for this. It’s not within the scope of this project to tell people how they should organize their settings files. Take that opportunity to showcase your individualism.
Import Error: No module named django.core.wsgi
Problem:
I have an nginx + django + wsgi setup. When I try to bring up the site I was getting the import error.
Solution:
Found a solution on stackoverflow. Turns out that wsgi was not able to find django in my site-packages. Following the pointers in the stackoverflow link below, I updated my sys.path and that fixed the issue.
import os import sys sys.path.append("/usr/share/python/<my_project_name>/lib/python2.7/site-packages") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<my_project_name>.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
Appengine – Appstats (python)
Setting up Appstats for python and djangoappengine.
Assuming you have your djangoappengine setup as explained here.
Now following the instructions from http://code.google.com/appengine/docs/python/tools/appstats.html
1. Add this to settings.py
MIDDLEWARE_CLASSES = ( 'google.appengine.ext.appstats.recording.AppStatsDjangoMiddleware', # ... )
2. Add this to app.yaml
builtins: - appstats: on
That’s it. Now start your server is running
python2.5 manage.py runserver
Point your browser to http://127.0.0.1:8000/_ah/stats/
You should see something like
Now add some data by visiting some pages eg http://127.0.0.1:8000/ (Do this in a different tab then refresh.) You should have some stats now.
That’s it. Very simple and straight forward instructions from the docs.
Djangoappengine – setting up
Objective:
Set up djangoappengine.
Steps:
1. Download appengine from http://code.google.com/appengine/downloads.html. (I am using Google App Engine SDK for Python – Linux/Other Platforms)
2. Download djangoappengine http://www.allbuttonspressed.com/projects/djangoappengine. The instructions on how to install are on the same page.
3. From the djangoappengine page.
Copy the following folders into your project (e.g., django-testapp):
* django-nonrel/django => /django
* djangotoolbox/djangotoolbox => /djangotoolbox
* django-autoload/autoload => /autoload
* django-dbindexer/dbindexer => /dbindexer
* djangoappengine => /djangoappengineThat’s it. Your project structure should look like this:
* /django
* /djangotoolbox
* /autoload
* /dbindexer
* /djangoappengine
Copy the folders from the zipped files into you django-testapp folder so that you have the same project structure as above. (In my case project was simply the django-testapp folder).
4. Within django-testapp folder run
user@computer:~$ cd /home/user/google_appengine/django-testapp user@computer:~/google_appengine/django-testapp$python2.5 manage.py runserver 127.0.0.1:8021 The Google App Engine SDK could not be found! Make sure it's accessible via your PATH environment and called google_appengine.
5. I needed to add google_appengine to where djangoappengine would find it.
From what I could tell, djangoappengine looks for google_appengine in various folders one of them being /usr/local/google_appengine. So I simply created a symbolic link to that folder. I had installed google_appengine in /home/user/google_appengine. So as root I created a symbolic link.
root@computer:~# ln -s /home/user/google_appengine /usr/local/google_appengine
6. Within django-testapp folder run
user@computer:~/google_appengine/django-testapp$python2.5 manage.py runserver 127.0.0.1:8021 WARNING:root:No ssl package found. urlfetch will not be able to validate SSL certificates. WARNING:root:Could not read datastore data from /home/user/google_appengine/django-testapp/.gaedata/datastore INFO:google.appengine.tools.appengine_rpc:Server: appengine.google.com WARNING:root:Could not read datastore data from /home/user/google_appengine/django-testapp/.gaedata/datastore INFO:root:Connecting to SQLite database '' with file '/home/user/google_appengine/django-testapp/.gaedata/rdbms' INFO:root:Running application ctst on port 8021: http://127.0.0.1:8021
NB. I am ignoring the warnings for now. I will post again once I figure out how to sort them out.
7. Open a browser and go to http://127.0.0.1:8021/.
If you get the above screen shot then you are good to go.
Update (15th September 2011):
1. The data store warning should go once you add data to your datastore.
2. For the follwing warning
WARNING:root:You are using the default Django version (0.96). The default
Django version will change in an App Engine release in the near future.
Please call use_library() to explicitly select a Django version. For more
information see
http://code.google.com/appengine/docs/python/tools/libraries.html#Django
Add to main.py as per app.yaml. In my case it is
– url: /.*
script: djangoappengine/main/main.py
os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘settings’
from google.appengine.dist import use_library
use_library(‘django’, ‘1.2’)
But when I do that I get this error
UnacceptableVersionError: django 1.2 was requested, but 1.3.0.final.0 is already in use
When I set it to use_library(‘django’, ‘1.3’) I get
ValueError: 1.3 is not a supported version for django; supported versions are [‘0.96’, ‘1.0’, ‘1.1’, ‘1.2’]
So I am ignoring the warning and not adding use_library to my main.py.