Login

local django test

Author:
jedie
Posted:
May 25, 2007
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

Sometimes I would like to test a codesnippet without a complete django environment. Here is a small "local test script" you can use for this ;)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    A small local django test.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Sometimes I would like to test a codesnippet without a complete
    django environment. Here is a small "local test script" you can
    use for this ;)

    http://www.djangosnippets.org/snippets/252/
    http://www.python-forum.de/topic-10600.html (de)

    :copyleft: 2007 by Jens Diemer
    :license: GNU GPL
"""

import os, sys

# insert django to the sys.path (optional):
#os.chdir("../django/")
#sys.path.insert(0, os.getcwd())

from django.core import management
from django.conf import settings

# setup a fake settings module
os.environ["DJANGO_SETTINGS_MODULE"] = "django.conf.global_settings"
settings.__file__ = "./django/conf/global_settings.py"

# Use a SQLite memory database for the local test:
settings.DATABASE_ENGINE = "sqlite3"
settings.DATABASE_NAME = ":memory:"

# modify some settings for you tests, if needed:
settings.INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
)

# Create the django database and environment:
print "init django, create tables...",
management.setup_environ(settings)
management.call_command('syncdb', verbosity=0, interactive=False)
print "OK\n"

#______________________________________________________________________________
# Now, you can play. Put your test code here:

from django.contrib.auth.models import User
from django import newforms as forms

UserForm = forms.form_for_model(User)
form = UserForm()
html_code = form.as_p()

print html_code

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.