Setting up our Django Project

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

Series Navigation<< Django – Starting to Build a Web App using Django