Setting up our Django Project

This entry is part 2 of 2 in the series Django Web App

In order to start developing our Django app, I’m going to help you begin by using Python best practices which includes setting up a virtual environment for this version of Python and this project’s requirements.  With virtualenvwrapper installed, run mkvirtualenv drecipes.  This will create a separate, isolated Python environment so you can continue using a different Python version on other projects as well as different versions of libraries.  For example, when I develop with older versions of Django and Python on some projects, virtualenv allows me to keep the two projects separate while still being compatible with each production environment.

Each time you want to develop for the drecipes project, you’ll run workon drecipes in order to activate this environment.

Create a requirements.txt file in your project directory.  Add these two lines to it:


Next, run pip install -r requirements.txt.  This will take the 2 requirements from our file and install them so they are ready to use.

With Django installed, run startproject drecipes.  This will create the initial Django project structure that looks similar to this:


In order to connect to our database, modify the file as follows:

     'default': {
-        'ENGINE': 'django.db.backends.sqlite3',
-        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
+        'ENGINE': 'django.db.backends.mysql',
+        'NAME': 'drecipes_codecrawl',
+        'USER': 'root',
+        'PASSWORD': 'Ma12sona',
+        'HOST': '',

NAME should be your database name, USER is the database user you create.  It is never a good idea to run a database as root, but since I’m only doing this locally, I am not concerned with a security issue.  The same goes for the PASSWORD not being encrypted or at least not stored directly in version control.  Normally, I store all of these in environment variables that can differ based on host, like test, stage, and production may all have different settings.

Then run python migrate as follows:

$ python migrate
Operations to perform:
  Apply all migrations: auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying sessions.0001_initial... OK

We’re not going to use the built in Django admin, so you can remove the admin lines from drecipes/, but you don’t have to.  I always do when my app is in production, and I’m not using it.

You should now be able to run python runserver to see site on localhost:8000 (see screenshot).

Initial Django app running

Django – Starting to Build a Web App using Django

This entry is part 1 of 2 in the series Django Web App

I’m going to try to keep this series focused on building a web app using Django and related technologies, so I’ll often skip over extraneous install help, especially on multiple operating systems.  Things like virtualenv, virtualenvwrapper, and database libraries can be particularly problematic on Windows and sometimes Mac, so I’ll leave any detail install/setup help for stackoverflow.  I am developing on Mac, but I have developed Django apps on Windows and Linux also, so I am familiar with all sorts of issues installing and running Python apps, if you end up needing help.

To start with, I’m going to provide a list of core development tools, libraries, and programs that help facilitate web development, especially in Python.  These will all make developing web apps easier in their own way.

  1. Python – I’m using Python 3.4.3 on a Mac
    $ python -V
    Python 3.4.3
  2. pip – The recommended tool to install Python packages/dependencies
  3. virtualenvwrapper – (Optional) Install using pip.  This is used to manage different Python projects that require different dependencies or versions of the same dependencies.
  4. A database – You can use SQLite which is built into Python.  I am going to use MySQL so I can utilize phpMyAdmin to display tables and their data.  Typically, I would use either MySQL or PostgreSQL, as of late I’ve become more accustomed to Postgres.  As you’ll see when we start creating the data models for our app, the database doesn’t matter for this example due to Django’s use of an ORM, which I’ll explain in more detail at a later time.
  5. Bootstrap – CSS for basic design and JavaScript functionality.

So aside from the libraries we’ll need, including Django, these are going to be our core dependencies to start building the Django web application.

A brief screencast of the web app we’ll build \/.

Next post, I’ll go over the initial Django project setup and creating some basic functionality.