Django local dev - mhulse/mhulse.github.io GitHub Wiki
Use pyvenv
instead of virtualenvwrapper
.
Before installation, check your python version and path for comparison’s sake.
Details here:
Install Python 3.x
using Homebrew:
# List available packages:
$ brew search python
# Install python3 package:
$ brew install python3
# Check python version:
$ python3 --version
Cool factor: pip3
comes with $ brew install python
! Nice!
IMPORTANT: Python 3.x
comes with pip3
. Python 2.x
comes with plain ol’ pip
. If using Python 3.x
, use pip3
.
Now you could check/compare stuff by running:
$ which python
/usr/bin/python
The above is the brew python.
... or:
$ python3
Python 3.4.2 (default, Oct 19 2014, 17:55:38)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
... which will start the interactive python interpreter and show you the version number (among other things).
Also, this:
$ brew info python3
... will give you lots of details as well.
Next, install virtualenv
and virtualenvwrapper
.
Some good instructions found here:
Installing Pip, Virtualenv & VirtualenvWrapper on OS X
pip3 install virtualenv
Once done, you can see if it’s installed via:
$ pip3 freeze
You can also type:
$ which virtualenv
/usr/local/bin/virtualenv
... or:
$ virtualenv
You must provide a DEST_DIR
Usage: virtualenv [OPTIONS] DEST_DIR
...
Next, install virtualenvwrapper
:
pip3 install virtualenvwrapper
Once that’s complete, check out what’s installed globally:
$ pip3 list
argparse (1.3.0)
pbr (0.10.3)
pip (1.5.6)
setuptools (2.1)
six (1.8.0)
stevedore (1.1.0)
virtualenv (1.11.6)
virtualenv-clone (0.2.5)
virtualenvwrapper (4.3.1)
Now, setup your virtualenvs
folder.
cd; mkdir .virtualenvs
Add these lines to your .bash_profile
:
# Python virtual environments:
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_VIRTUALENV=`which virtualenv`
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
export VIRTUALENVWRAPPER_PYTHON=`which python3`
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true
if [ -r /usr/local/bin/virtualenvwrapper.sh ](/mhulse/mhulse.github.io/wiki/--r-/usr/local/bin/virtualenvwrapper.sh-); then
#source /usr/local/bin/virtualenvwrapper.sh
source `which virtualenvwrapper.sh`
else
echo "WARNING: Can't find virtualenvwrapper.sh"
fi
Open a new terminal window and type:
$ mkvirtualenv
... to see if everything is setup properly.
Now, navigate to your local development location:
$ cd dev/
Make virtual environment:
$ mkvirtualenv FOO --no-site-packages
# Python 3:
$ mkvirtualenv --python=/usr/local/bin/python3 FOO --no-site-packages
This will create and activate the environment.
Check Python version:
$ python -V
Python 3.4.2
Exit the environment using:
$ deactivate
Activate the environment using:
$ workon FOO
List available environments:
$ workon
With your FOO
environment activated, install Django:
$ pip3 install django
Create the Django project:
$ django-admin.py startproject FOO
Note that I’m naming my Django project the same thing as my virtual environment.
At this point, your folder structure will look like:
FOO/
└──manage.py
└──FOO/
└──__init__.py
└──__init__.py
└──settings.py
└──urls.py
└──wsgi.py
Create your app:
$ python manage.py startapp baz
Here’s what things should look like now:
FOO/
└──manage.py
└──baz/
└──migrations/
└──__init__.py
└──admin.py
└──models.py
└──tests.py
└──views.py
└──FOO/
└──__init__.py
└──__init__.py
└──settings.py
└──urls.py
└──wsgi.py
Create tables in the database for existing boilerplate apps (like the admin):
$ python manage.py migrate
Start up the development server:
$ python manage.py runserver
… and then visit: http://127.0.0.1:8000/
You should see this:
Don’t forget to start your Postgres database!
Next, create your models.
Add app to the INSTALLED_APPS
tuple in your project’s settings.py
:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'baz',
)
In a new terminal window/tab, cd
to your project folder and run:
$ python manage.py makemigrations baz
# 0001 is migration number which you see output from above command:
$ python manage.py sqlmigrate baz 0001
# Check to make sure all is good:
$ python manage.py check
# Update your models:
$ python manage.py migrate
If you get any errors, fix ’em and repeat steps above.
If all is good, restart the development server in the other window:
$ python manage.py runserver
That’s it! Up next, the admin.
Run:
$ python manage.py createsuperuser
… and follow the on-screen instructions.
Log in to the development server‘s admin site: http://127.0.0.1:8000/admin
You should be able to navigate your app‘s models. Click around. Check it out. :+1:
If you see any form labels that are bold, that means they are required; if you don’t want that, then go back to your model and add null=True, blank=True
to those fields to make them not required. For example:
part_number = models.IntegerField(null=True, blank=True,)
If you notice that your model fields are not very well organized (like, the description field is on the top), well, we can fix that too! Crack open admin.py
.
There’s a ton you can do here. More than I can list. Here’s the docs.
One example would be an “inline” model (this means, having one model show inside another model, which allows you to add an unlimited (by default) number of child entries inside of another model.
For example, here’s a mode:
class Quote(Base):
name = models.CharField(max_length=255, blank=True)
quote = models.ForeignKey('Lead', blank=True, null=True,)
In admin.py
, do this:
from django.contrib import admin
from baz.models import *
class QuoteAdminInline(admin.TabularInline):
#----------------------------------
# Fields:
#----------------------------------
fields = (
'name',
'notes',
)
model = Quote
extra = 1
class LeadAdmin(admin.ModelAdmin):
inlines = [
QuoteAdminInline,
]
class QuoteAdmin(admin.ModelAdmin):
pass
admin.site.register(Lead, LeadAdmin)
admin.site.register(Quote, QuoteAdmin)
Now you should get this:
That‘s an inline model. You can now add as many dynamic entries as you want. :+1: