Sometimes i like to try things out in a blank django project, so i created a python script which creates an out of the box working project skeleton for me:
$ djangoskel.py project_name
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #!/usr/bin/python
import os
import sys
import pprint
from django.core.management import setup_environ
# personal data
USER = 'vyvyan'
EMAIL = 'Vyvyan [email protected]'
PASSWORD = 'SPG'
# preparing environment
PROJECT = sys.argv[1]
CWD = os.getcwd()
PROJECT_ROOT = '%s/%s' % (CWD, PROJECT)
PYTHON_PATH = '%s/%s/src' % (CWD, PROJECT)
SRC_PATH = '%s/%s/src/%s' % (CWD, PROJECT, PROJECT)
sys.path.append(PYTHON_PATH)
# replacement values for settings
my_settings = dict(
ADMINS = ((USER, EMAIL),),
TIME_ZONE = 'Europe/London',
DATABASE_ENGINE = 'sqlite3',
DATABASE_NAME = '%s/%s.db' % (PROJECT_ROOT, PROJECT),
MEDIA_ROOT = '%s/media_root' % PROJECT_ROOT,
MEDIA_URL = '/media_root',
TEMPLATE_DIRS = ('%s/templates' % PROJECT_ROOT,),
)
# creating project dirctories
os.mkdir(PROJECT_ROOT)
sub_dir = ('src', 'site_media', 'templates', 'media_root',
'site_media/images', 'site_media/css', 'site_media/js')
for d in sub_dir:
os.mkdir('%s/%s' % (PROJECT_ROOT, d))
# run startproject
os.chdir(PYTHON_PATH)
os.system('django-admin.py startproject %s' % PROJECT)
# rewrite settings.py
settings = __import__('%s/settings' % PROJECT)
for k, v in my_settings.items():
setattr(settings, k, v)
# enabling admin app
setattr(settings, 'INSTALLED_APPS', settings.INSTALLED_APPS + ('django.contrib.admin',))
settings_file = '%s/settings.py' % SRC_PATH
pp = pprint.PrettyPrinter(indent=4, width=50)
f = file(settings_file, 'w')
for attr in dir(settings):
if not attr.startswith('__'):
f.write('%s = ' % attr)
f.write(pp.pformat(getattr(settings, attr)))
f.write('\n\n')
f.close()
# running snycdb
os.chdir(SRC_PATH)
os.system('python manage.py syncdb --noinput')
#setting up django env
setup_environ(settings)
# creating superuser
from django.contrib.auth.models import User
u = User.objects.create_user(USER, EMAIL, PASSWORD)
u.is_staff = True
u.is_active = True
u.is_superuser = True
u.save()
print "Superuser created successfully."
# enabling admin in urls.py
urls = '%s/urls.py' % SRC_PATH
content = ''
f = file(urls)
for line in f:
if line.find('admin/') >=0:
content += line[2:]
else:
content += line
f.close()
f = file(urls, 'w')
f.write(content)
f.close()
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.