Login

Automatically set up a project

Author:
madprops
Posted:
July 29, 2012
Language:
Python
Version:
1.4
Score:
-1 (after 1 ratings)

This is a script to automatically set up a django project It takes only one argument for the project name This works for Django 1.4

It will create the following directory structure: /project /server (app name) /media: /html /css /js /img

  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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/python

# This is a script to automatically set up a django project
# It takes only one argument for the project name
# This works for Django 1.4

import os
import sys

if len(sys.argv) == 1:
	sys.exit('    You need to provide the project\'s name')
project_name = sys.argv[1]
os.system('django-admin.py startproject ' + project_name)
os.chdir(os.getcwd() + '/' + project_name)
os.system('python2 manage.py startapp server')
os.system('rm ' + project_name + '/settings.pyc')
os.system('mkdir media')
os.system('mkdir media/html')
os.system('mkdir media/css')
os.system('mkdir media/js')
os.system('mkdir media/img')

views = open('server/views.py', 'w')

s = """from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
from django.core.context_processors import csrf
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login as auth_login, logout as auth_logout
from django.contrib.auth.models import User
from server.models import *

def create_c(request):
	c = {}
	c.update(csrf(request))
	c['user'] = request.user
	return c

def main(request):
	c = create_c(request)
	return render_to_response('main.html', c)
"""

views.write(s)
views.close()

urls = open(project_name + '/urls.py', 'w')

s = """from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
	url(r'^admin/', include(admin.site.urls)),
	(r'^media/(?P<path>.*)$', 'django.views.static.serve', {{'document_root':'{path}/media/'}}),
	(r'^$', 'server.views.main'),
)
"""

s = s.format(path = os.getcwd())

urls.write(s)
urls.close()

settings = open(project_name + '/settings.py', 'w')

s = """DEBUG = True

TEMPLATE_DEBUG = DEBUG

ADMINS = ()

MANAGERS = ADMINS

DATABASES = {{
    'default': {{
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '{path}/db',
        'USER': 'db',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }}
}}

TIME_ZONE = 'America/Chicago'

LANGUAGE_CODE = 'en-us'

SITE_ID = 1

USE_I18N = True

USE_L10N = True

USE_TZ = True

MEDIA_ROOT = ''

MEDIA_URL = ''

STATIC_ROOT = ''

STATIC_URL = '/static/'

STATICFILES_DIRS = ()

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

SECRET_KEY = '2bwk)z=_@+e1p(aoczvyoeaqs^g0fzx1)m9jfrpnch++-0l32!'

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = '{project_name}.urls'

WSGI_APPLICATION = '{project_name}.wsgi.application'

TEMPLATE_DIRS = ('{path}/media/html')

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'server',
)
"""
s = s.format(path = os.getcwd(), project_name=project_name)

settings.write(s)
settings.close()

mainhtml = open(os.getcwd() + '/media/html/main.html', 'w')

s = """<!doctype>
<html>
<head>
	<title>main</title>
    <link rel='stylesheet' href='/media/css/main.css'>
    <script src='/media/js/jquery.js'></script>
	<script src='/media/js/main.js'></script>
	<link rel="icon" href="favicon.ico" type="image/x-icon"> 
	<link rel="shortcut icon" href="/site_media/favicon.ico" type="image/x-icon">
</head>
<body>
	ohai
</body>
</html>
"""

mainhtml.write(s)
mainhtml.close()

maincss = open(os.getcwd() + '/media/css/main.css', 'w')

s = """html
{
	font-size:18px;
	color: white;
	background: black;
}
a:link, a:visited, a:hover
{
	color: blue;
	text-decoration: none;
} 
"""
maincss.write(s)
maincss.close()

mainjs = open(os.getcwd() + '/media/js/main.js', 'w')
mainjs.close()

models = open(os.getcwd() + '/server/models.py', 'w')

s = """from django.db import models
from django.contrib.auth.models import User
import datetime

class Sample(models.Model):
	user = models.ForeignKey(User)
	name = models.CharField(max_length=100, unique=True)
	content = models.TextField(max_length=10000, default=None, null=True)
	date = models.DateTimeField(default=datetime.datetime.now())
"""

models.write(s)
models.close()

try:
    os.chdir(os.getcwd() + '/media/js')
    os.system('wget -O jquery.js http://code.jquery.com/jquery-1.8.0.min.js')
except:
    pass

More like this

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

Comments

doomatel (on July 29, 2012):

Use project templates, Luke

#

Please login first before commenting.