Django Series

Lesson 1: Start

Lesson 2: Git

Lesson 3: MVC

Lesson 4: Forms

Lesson 5: Models

Appendix i: Setup

Appendix ii: Shell

Appendix iii: TDD

How to start developing a Django project?


Django is the most popular Python web framework to date. The tagline “For perfectionist with deadlines” perfectly sums up what you can expect if you start using it. You have control over every aspect of your application, but it require some serious commitment to wield that power. When in doubt check out the excellent documentation, which will definitely solve most of your challenges.


This blog post is about how to start developing a Django Project on your computer. I am assuming you are using Linux operating system, specifically Ubuntu. Also, I had absolute beginners in mind. This is a technical tutorial about what exact commands you need to put in, to get up and running. That said, let's start:

Basic Directory Structure


First, create a directory you want your project in:

mkdir -p DjangoTutorial/{media,static,virtualenv,database} (don't leave spaces in the last part)


This command creates the whole directory tree for you:

DjangoTutorial/

    database/

    media/

    static/

    virtualenv/


Navigate into DjangoTutorial, then initiate virtualenv with the following command:

cd DjangoTutorial

virtualenv –python=python3 ./virtualenv


Virtual Environment will make sure, that you will have consistent environments when you deploy your application to different servers. You will install plugins and packages, and you will replicate those installs on a remote server automatically.


If it doesn't find virtualenv (virtualenv command not found), you should do do:

pip install virtualenv.


If it doesn't even find pip, you should put in the following:

sudo apt-get install python-pip python-dev build-essential (that will take some time to complete).


Once ready, activate virtualenv with the following command:

source ./virtualenv/bin/activate


You should see (virtualenv) before your username like so:

(virtualenv) ***@***:~/***/DjangoTutorial/


Let's install Django:

pip install django==1.10

This command will make sure, that you will have the exact same Django as mine and there will be no bugs based different versions.


Let's startup our django project.

django-admin startproject MyTutorial


You should see a new folder in your DjangoTutorial directory.

database/

media/

virtualenv/

static/

MyTutorial/

    manage.py

    MyTutorial/

        __init__.py

        settings.py

        urls.py

        wsgi.py


Rename MyTutorial to source, and navigate into it:

mv MyTutorial source

cd source

 

Also it is important to save your dependencies into your requirements text file. This way you will make your deployment easy.

pip freeze > requirements.txt

Basic Configuration

Now, it's time for some configuration:

touch MyTutorial/local_settings.py


Why should you use local settings? If you deploy your application to a server you might use different database, also you may have a version on your computer in which you will develop and another one running live. When you will update your live application, only the relevant pieces will be copied and your locals stay the same.


Cut the following variables into MyTutorial/local_settings.py from MyTutorial/settings.py:

SECRET_KEY, DEBUG, ALLOWED_HOSTS, DATABASES. Then change DATABASES, NAME line into:


*** MyTutorial/local_settings.py ***

'NAME': os.path.join(BASE_DIR, '..','database','db.sqlite3')

To the top of the MyTutorial/local_settings.py write the import directives:

import os

from MyTutorial.settings import BASE_DIR


So the file should look like this:

*** MyTutorial/local_settings.py ***

import os

from MyTutorial.settings import BASE_DIR

# SECURITY WARNING: keep the secret key used in production secret!

SECRET_KEY = 'rf@7y-$2a41o+4&z$ki0&=z)(ao=@+$fseu1f3*f=25b6xtnx$'

# SECURITY WARNING: don't run with debug turned on in production!

DEBUG = True

ALLOWED_HOSTS = []

# Database

# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {

  'default': {

  'ENGINE': 'django.db.backends.sqlite3',

  'NAME': os.path.join(BASE_DIR,'..','database','db.sqlite3'),

  }

}

In the meantime, change MyTutorial/settings.py:

Replace STATIC_URL with the following:

*** MyTutorial/settings.py ***

STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '..', 'static'))

STATIC_URL = '/static/'

# Media files

MEDIA_ROOT = os.path.join(BASE_DIR, '..', 'media')

MEDIA_URL = 'media/'


To the bottom also write:

*** MyTutorial/settings.py ***

...

from MyTutorial.local_settings import SECRET_KEY, DEBUG, ALLOWED_HOSTS,DATABASES


Now setup the database. Current working directory is DjangoTutorial/source. Into the command line, write:

python3 manage.py migrate


That command starts up the database, check into the database folder:

ls ../database

it should give you back:

db.sqlite3


Startup the server:

python3 manage.py runserver


Now, open up your firefox and type in: localhost:8000. Should see the welcome message: “It worked!”.

Stop the server from the terminal with Ctrl-C.

Version Control

Before you finish we should setup version controlling.

In the DjangoTutorial/source directory put in the command:

git init .


If it doesn't find git, you can put: sudo apt-get install git

Check if you have a .git directory:

ls -la

Should look like:

drwxrwxr-x 4 david david 4096 nov 3 15:30 .

drwxrwxr-x 7 david david 4096 nov 3 13:00 ..

drwxrwxr-x 7 david david 4096 nov 3 15:30 .git

-rwxrwxr-x 1 david david 808 nov 3 12:05 manage.py

drwxrwxr-x 3 david david 4096 nov 3 15:18 MyTutorial


Now, you will setup the gitignore file.

echo “*/migrations/*” > .gitignore

echo “*.pyc” >> .gitignore

echo “local_settings.py >> .gitignore

Gitignore as the name implies will ignore every file involved from version controlling, you should always setup this before your first commit, at the beginning of the project. You want to include files that specific to your environment and will change when deployed to a remote server.


git status

You can view files which needs to be committed.

git status MyTutorial/

Should see only these four files:

MyTutorial/__init__.py

MyTutorial/settings.py

MyTutorial/urls.py

MyTutorial/wsgi.py

If there is difference, check out .gitignore,

cat .gitignore

Should see this inside:

*/migrations/*

*.pyc

*/local_settings.py


Everything is ready to set:

git add .

git status (you will see that files are cached to the next commit:all green)

git commit -m “MyTutorial initial setup”


If you want to stop the server, press Ctrl-C. You can exit from virtualenv by typing:

deactivate


And now you are officially started your journey into Django Mastery!

 

To recap: you made the proper directory structure for Django projects. You started the Django project with the django-admin startproject MyTutorial command. You have a basic skeleton of a project. You configured it to separate your local settings, therefore it will ease you work when it comes to deployment, and also you made a snapshot of this with a version control software: git.


Congratulations!


Tags: django , tutorial , beginner , git , virtualenv , svc