Start your own RSS reader project, part 2.

So, in the previous post, you have gathered all the tools you need. You can now focus on the project. Thus, before defining our User Stories, we need to create the project. This will start by initiating a project trough Django. This step is nicely described on the Django Tutorial, so in this post, we’ll focus on our specific need. It will be necessary to understanding the terminology used by Django and more specifically what is a Project and what is an Application.

Understanding the terms

A Project is an instance of Django with its own configurations and settings. After creating a project, we’ll still need to create an application, that is where our reader will be defined. Consider the project as being your website, and the application to be exactly what it is, one functional unit.

Initiating the project

So the project is not the RSS reader. The project is most likely to be MyPrivateSite, and the reader will be an application. Let’s create our project, on the command line in your working directory, just type the following:

django-admin.py startproject myprivatesite

This command will create the myprivatesite directory in your current directory. The goal of this tutorial is not to enter in the details of Django, neither to repeat what was better explained elsewhere. If you are interested in the details of what was created (and if you want to use Django, you’ll better be), just check the first part of the Django tutorial. For the purpose of this tutorial, just remember that most of the configuration will be done in the settings.py file, the url patterns will be defined in the urls.py file, and you’ll need the wsgi.py file if you want to deploy with WSGI.

The manage.py file will now be the central command for all the administrative tasks related to your project. Actually, manage.py is just a wrapper for django-admin.py, the command you have used to create the project. Manage.py takes care of all the settings related to your project before delegating the execution of the command to django-admin.py.

First test of the project

At this point, you may want to see what is done. Not that much indeed… You can launch the development server with the command

python manage.py runserver

Just go to http://localhost:8000. This is the standard Django’s It work page. We still have work to do to display something on this page. Lets start by creating our first application.

Creating our application

We’ll call manage.py. Let’s say that our application’s name will be reader, just launch the following command from your project’s directory:

python manage.py startapp reader

This will create a new directory in your project. This is more specifically a package, and it should contain everything your application would need to work.

One important thing you have to know about Django, is that the framework was designed for reusability. An application is a package you should be able to use in any project, or distribute at will. So the last command created a populated directory, and actually, that is all it did. At this point, your project is unaware of this application existence. Did I wrote that manage.py was just a wrapper ? Well, just try:

django-admin.py startapp mytestapp

Yes, you just created a new application which template is exactly the same as the one created through manage.py. An application is not linked to a project. A regular Django project file tree will consist of the project as a root directory, and the apps within. But if you want, you can create and work on an application without ever creating a project. Yes, that is useless, but it also mean that an application can be located anywhere with the only constraint of being in the Python Path.

So, first, you can safely clean your project and delete the mytestapp directory and all its content. Second, at this point, the project is still unaware of your application. Just restart your development server and check the page. Yep, still the same. We’ll skip the standard display of a simple Hello World page and just set up everything we really need. In that purpose, we need to configure 2 things.

Configuring the project

As you will use a database, you need to configure it’s settings in the setting.py file. This is a standard Python module, the settings are defined in the module-level variables. During development, we’ll use the shipped SQLite engine. So, make sure your DATABASE entry looks like:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/Users/username/workspace/myprivatesite/sqlite.db',
        '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.
    }
}

The meaning of the ENGINE key is straightforward, we are telling Django to use the SQLite engine. NAME is the full path to the database file. In this case, it is stored at the root of the project. Of course, you can define it anywhere you want.

The settings.py module is also the place where the applications are imported. Look for the INSTALLED_APPS entry and add the last item:

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',
    'reader',
)

Those settings will not change anything on the resulting page and the project is nearly complete. We just need to run one last command:

python manage.py syncdb

This will create all the necessary database tables. We’ll see it with the model once we will have designed it. And this is coming in the next post.

In the meantime, feel free to post any question in the comments bellow.

About Darko Stankovski

Darko Stankovski is the founder and editor of Dad 3.0. You can find more about him trough the following links.

Darko Stankovski

Darko Stankovski is the founder and editor of Dad 3.0. You can find more about him trough the following links.

You may also like...

4 Responses

  1. March 27, 2013

    […] ← Previous Next → […]

  2. March 29, 2013

    […] in the database creation subject, you may wonder what was the action of the syncdb we executed in the previous post. Syncdb creates all the tables for all apps defined in INSTALLED_APPS. So, the previous execution […]

  3. April 11, 2013

    […] admin site is not activated by default. Do you remember the line 9 of the INSTALLED_APPS code snippet ? To activate the admin site, you need […]

  4. April 16, 2013

    […] ← Previous Next → […]

Leave a Reply