Django 1.8 starters guide

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

Setting up locally

Make sure your vagrant box can access port 8000
Vagrantfile

config.vm.network "forwarded_port", guest: 8000, host: 8000
config.vm.synced_folder "shared", "/home/vagrant/"

Installing dependencies

sudo apt-get install python-pip 
sudo pip install Django

Check versions installed

pip freeze

Go to the folder you want your web folder in

django-admin.py startproject myproject
cd myproject
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver 0.0.0.0:8000

If you got no errors you can open your browser and view the starting django project
http://127.0.0.1:8000/
http://127.0.0.1:8000/admin

Sometimes you may need to install requests package

pip install requests

Setup your DB

python manage.py syncdb // depreciated in 1.9
python manage.py makemigrations
python manage.py migrate

Run django in the shell

python manage.py shell
exit() # to exit from shell

Creating your static files

python manage.py collectstatic

Typical folders to add:

/templates/
/static/
/static/static_root/
/static/static_dirs/
/static/static_dirs/css/
/static/static_dirs/img/
/static/static_dirs/js/

settings.py changes

STATIC_URL = ‘/static/’

STATIC_ROOT = os.path.join(BASE_DIR, ‘static’, ‘static_root’)

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, ‘static’, ‘static_dirs’),
)

TEMPLATES = [
    {
        ‘DIRS’: [‘templates’], } ]

to add apps
INSTALLED_APPS

to add middleware
MIDDLEWARE_CLASSES

If your app is already running and you need to stop it you can kill the process

lsof -i :8000
kill [PID]

Create your module

python manage.py startapp myapp

Using git ignore with django
https://github.com/github/gitignore/blob/master/Python.gitignore

Using MySQL

sudo apt-get install mysql-server
sudo apt-get install python-mysqldb
sudo service mysql start

To access mysql through command line

mysql -uroot -p

Create your DB manually (recommended utf-8)

Edit your settings.py file

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': ‘dbname’,
        'USER': ‘root’,
        'PASSWORD': ‘root’,
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

Adding your favicon

pip install django-favicon

settings.py file

INSTALLED_APPS = (
    ...
    'favicon',
)

urls.py file

urlpatterns = patterns('',
    ...
    url(r'^', include('favicon.urls')),
)

Internal IPs

If you are using some tool like the debug toolbar then understand that your internal IP maybe different

To check what your IP is add this to your view:

print request.META.get('REMOTE_ADDR', None)

You can then see the IP that requests come from.
In my case it was 10.0.2.2 so I updated my INTERNAL_IPS in settings.py as:

INTERNAL_IPS = ('127.0.0.1', '10.0.2.2', '0.0.0.0', )

Tests

To run all tests

python manage.py test

To run only certain tests

python manage.py test myfolder/

Coverage

To keep an eye on your tests coverage you can

sudo pip install coverage
coverage run --include='./*' manage.py test
coverage report

Resources
https://docs.djangoproject.com/en/1.8/ref/models/fields/

Useful additions:
http://www.mailgun.com
https://django-payments.readthedocs.org
https://github.com/joke2k/django-faker
http://www.django-rest-framework.org
https://docs.djangoproject.com/en/dev/ref/models/fields/#help-text
http://django-extensions.readthedocs.org/en/latest/
https://github.com/django-debug-toolbar/django-debug-toolbar
https://gitlab.com/rosarior/awesome-django

1 comment for “Django 1.8 starters guide

  1. October 11, 2015 at 10:09 pm

    I’d also recommend virtualenv as a way to keep your set of libraries seperate from the OS set. Not strictly required when working in a vagrant, but it does mean you don’t have to sudo every library install.

Leave a Reply

Your email address will not be published. Required fields are marked *