DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Standalone Django For Testing

02.13.2010
| 2683 views |
  • submit to reddit
        // For running django directly rather than running it through a server.
// I use it for testing.

# Put this at the top of your module, before you import
# any of the django code. For an in-depth discussion, see
# James Bennett's blog - http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/

if __name__=='__main__':
    # Path to my django project.
    import sys
    sys.path.insert(0,r'C:\Documents and Settings\CCM\Desktop\my_project')
    from django.core.management import setup_environ    
    from my_projects import settings
    setup_environ(settings)

# An example import. By adding the project path to sys.path, the imports
# do not have to include the project name (e.g. my_project.an_app.models),
# which makes it easy to insert this code in another project.
from an_app.models import AModel