Learning to Program Django: Getting Started

Django is a set of programming tools that allows you to quickly and easily build a website. If you were building a house you would use power tools such as table saws and nail guns to quickly build your house.  Power tools not only save you a lot of time; they also help you do a better job. Think of Django is a large collection of power tools that will enable you to build an entire website.  If you want to program for the modern web I highly recommend you build your house with a modern set of power tools. If you don't use Django then use Ruby on Rails. Not convinced? Read "Why you should only use modern programming tools for building a website!" You must follow along and type out the commands to learn. Get started by first following the quick install guide to have access to Django.
scott$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print django.get_version()
1.4.1
>>>
If you can run these command then you can begin. This creates a django project and your first application.
django-admin.py startproject myproject
cd myproject
python manage.py startapp city
You create a number of empty files which you will need to edit with a text editor such as Notepad on Windows or (TextWrangler/Sublime Text 2) on Mac. Now you need to configure your django project's database. This is were all the data we create and the django data will be stored. Open your myproject/settings.py and add sqlite3 as your database engine and cities.db as the name of your database file.
DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'cities.db', # Or path to database file if using sqlite3.
    'USER': '', # Not used with sqlite3.
    'PASSWORD': '', # Not used with sqlite3.
    'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '', # Set to empty string for default. Not used with sqlite3.
    }
}
This creates your default django apps.
scott$ python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'scott'):
E-mail address: anyuser@hotmail.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
Woot! Lets create your City app's first data Model. Open city/models.py and add the code.
class City(models.Model):
    name = models.CharField(max_length=255)
So what hell is this? Modern databases use tables to store data and a 'City' Model will store the 'name' of the city we add. Lets added it to our django app. Open myproject/settings.py, remove the # from infront of 'django.contrib.admin', and add 'city'. Make sure the text is the same or you might get an error. Django is a "python" power tool and spacing matters in python.
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'city',
)
In you're terminal run "python manage.py syncdb" to created your City app's database table which is city_city. It is named this way because your app is City and model is City. If we'd used Cities app and City model it would have been cities_city.
scott$ python manage.py syncdb
Creating tables ...
Creating table django_admin_log
Creating table city_city
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
In you're terminal run "python manage.py runserver" to start your server!
scott$ python manage.py runserver
Validating models...
0 errors found Django version 1.4.1, using settings 'myproject.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C. In your web browser go to http://127.0.0.1:8000/ # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', ... url(r'^admin/', include(admin.site.urls)), ) In your web browser go to http://127.0.0.1:8000/admin/  then login with the username and password you created earlier. You should now see User and Groups but our City app is missing. Create a new file called city/admin.py
from django.contrib import admin
from city.models import City

class CityAdmin(admin.ModelAdmin):
    pass

admin.site.register(City, CityAdmin)
Go back to http://127.0.0.1:8000/admin/ and you'll see your City app! So with very little heartache and a little bit of configuration you have create a project with one application that saves data to a database. Access to they administration interface is secured and you have all the CRUD you need. (Create, Read, Update, Delete) functionality most application need.

Author:

Previous: Learning to Program My Advice as a Mentor

Tags: django

Comments powered by Disqus