Version 11 (modified by 19 years ago) ( diff ) | ,
---|
Simple Bash Script To Setup Development Environment
Pure Python Script
#!/bin/sh export DJANGO_PROJECT="$(basename `pwd`)" export PYTHONPATH="$(dirname `pwd`)" export DJANGO_SETTINGS_MODULE="${DJANGO_PROJECT}.settings" ./script.py
Don't know why the sh wiki processor ain't workin', maybe the server doesn't have enscript
installed, so here's a plain and not that neat view of the script:
#!/bin/bash # Project Name, for example: # DJANGO_PROJECT="$(basename `pwd`)" #DJANGO_PROJECT='' # Django's HTTP server settings #SERVER_ADDR="192.168.0.2" #SERVER_PORT="8080" # Database Settings DJANGO_DB_NAME="${DJANGO_PROJECT}" DJANGO_DB_USER='' DJANGO_DB_PASS='' # Project's unique ID setting DJANGO_SECRET_KEY='YOUR_SECRET_UNIQUE_ID_HERE' # Update our Python path if [ "$(echo $PYTHONPATH | grep $(dirname `pwd`))" == "" ]; then PYTHONPATH="$(echo $PYTHONPATH):$(dirname `pwd`)" fi" # Setup DJANGO_SETTINGS_MODULE DJANGO_SETTINGS_MODULE="${DJANGO_PROJECT}.settings" # Check argument passed to script, if '-S', it starts Django's HTTP server if [ "$1" == "-S" ]; then # If SERVER_ADDR is not setup, use the default if [ -z "${SERVER_ADDR}" ]; then SERVER_ADDR="localhost" fi # If SERVER_PORT is not setup, use the default if [ -z "${SERVER_PORT}" ]; then SERVER_PORT="8000" fi # Start the Django's HTTP Server django-admin.py runserver ${SERVER_ADDR}:${SERVER_PORT} \ --settings="${DJANGO_PROJECT}.settings" fi
#!/bin/bash # Project Name, for example: # DJANGO_PROJECT="$(basename `pwd`)" #DJANGO_PROJECT='' # Django's HTTP server settings #SERVER_ADDR="192.168.0.2" #SERVER_PORT="8080" # Database Settings DJANGO_DB_NAME="${DJANGO_PROJECT}" DJANGO_DB_USER='' DJANGO_DB_PASS='' # Project's unique ID setting DJANGO_SECRET_KEY='YOUR_SECRET_UNIQUE_ID_HERE' # Update our Python path if [ "$(echo $PYTHONPATH | grep $(dirname `pwd`))" == "" ]; then PYTHONPATH="$(echo $PYTHONPATH):$(dirname `pwd`)" fi # Setup DJANGO_SETTINGS_MODULE DJANGO_SETTINGS_MODULE="${DJANGO_PROJECT}.settings" # Check argument passed to script, if '-S', it starts Django's HTTP server if [ "$1" == "-S" ]; then # If SERVER_ADDR is not setup, use the default if [ -z "${SERVER_ADDR}" ]; then SERVER_ADDR="localhost" fi # If SERVER_PORT is not setup, use the default if [ -z "${SERVER_PORT}" ]; then SERVER_PORT="8000" fi # Start the Django's HTTP Server django-admin.py runserver ${SERVER_ADDR}:${SERVER_PORT} \ --settings="${DJANGO_PROJECT}.settings" fi
You also need to make some changes to your ~/.bashrc
. Add the following to it:
export DJANGO_PROJECT='' export DJANGO_DB_NAME='' export DJANGO_DB_USER='' export DJANGO_DB_PASS='' export DJANGO_SECRET_KEY='' export DJANGO_SETTINGS_MODULE=''
export DJANGO_PROJECT='' export DJANGO_DB_NAME='' export DJANGO_DB_USER='' export DJANGO_DB_PASS='' export DJANGO_SECRET_KEY='' export DJANGO_SETTINGS_MODULE=''
To use it, just source it to update environment variables:
source env.sh
To run the server:
./env -S
This is of course, a file that you won't send/add to your revision control system, it has private data.
Now, on your project's settings.py
, you can use(these are just parts of settings.py
of course, the ones that need change from the default):
import os DATABASE_NAME = os.environ['DJANGO_DB_NAME'] DATABASE_USER = os.environ['DJANGO_DB_USER'] DATABASE_PASSWORD = os.environ['DJANGO_DB_PASS'] SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
And now, you can safely commit settings.py
to your revision control system without double checking to see if you forgot to delete the private parts.