A Django Blog: 1. Creating and Configuring the Project and Application
I’m assuming that you are relatively new to Python and django; and that you have them installed. I will try to make the material of this tutorial as simple as possible.
Note, if you can’t see the instructions below, please click the title of this post. I’ll get this fixed when I get a chance.
Create a Projects Folder
The first thing we need is somewhere to place your project. Open up a terminal (Windows: Start -> Run -> Enter “cmd” and hit enter; Mac: Finder -> Applications -> Utilities -> Terminal) and enter one of the following commands:
# Mac and Linux Only mkdir ~/Projects # Windows Only cd C:\ md Projects # All Systems cd Projects
Also, don’t close the Terminal. We will use it often.
Creating the Project
The next step is to create the project. Enter the following commands:
django-admin.py startproject myblog
cd myblogThe first command tells django to create an empty project called myblog (we will call the application blog). The project directory contains __init__.py, manage.py, settings.py, and urls.py. __init__.py is an empty file and usually will be empty, but it is needed for django to run. manage.py will be used through the rest of the project rather than django-admin.py. settings.py contains all of our configuration settings. And urls.py holds the information that tells which URL goes to which view and what information is sent to the view.
The second command, unsurprisingly, changes our current directory to the Projects/myblog folder. This way, we can easily create the blog application.
Creating the Application
And now for another quick command to create the blog application:
python manage.py startapp blog
This command simply creates a new application called blog. For the observant, you may notice that it really creates a new folder called blog under the myblog folder. Within the blog folder, there are four files: __init__.py (same as before), models.py (more on this file later), tests.py (we will not cover this file in the tutorial), and views.py (more on this file later). Also, take note that the name of the application is different from the name of the project. These cannot be the same. In fact, django will stop you if you try to make them the same.
Running the Server
Alright, let’s take a quick break and see something that looks nicer. Run the following command:
python manage.py runserver
django comes with a web-server built-in. Use it only for development purposes as it is not meant to function in a production environment. To view it, go into your web browser and go to http://localhost:8000/. You should see a nice page that claims that it works (and it does!). Okay, now let’s continue. Go back into the terminal and kill the server (Ctrl + C).
Configuring myblog
Okay, now open the settings.py file from the myblog folder in your text editor. Slightly confusing at first, isn’t it? Well, let’s step through this file one section at a time.
# Django settings for myblog project. DEBUG = True TEMPLATE_DEBUG = DEBUG
This first section of the file simply sets the DEBUG and TEMPLATE_DEBUG variables to True. By setting DEBUG to True, you will see helpful (yet sometimes confusing) error messages appear when something goes wrong. The second line (TEMPLATE_DEBUG = DEBUG) simply sets TEMPLATE_DEBUG to True by copying it from DEBUG. If you turn DEBUG off, then django is programmed to send you emails when something fails with the information needed to fix the error in it.
ADMINS = ( ('Colton J. Provias', 'nobody@coltonprovias.com'), ) MANAGERS = ADMINS
There are two more variables in this bit of code. They are simply lists of people who run the site. Go ahead and fill in your name and email address as I have done above in the ADMINS list. If you want to add more ADMINS, just copy and paste the line, making sure everything lines up properly with indents.
The MANAGERS list simply is a copy of the ADMINS list.
DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. DATABASE_NAME = 'dev.db' # Or path to database file if using sqlite3. DATABASE_USER = '' # Not used with sqlite3. DATABASE_PASSWORD = '' # Not used with sqlite3. DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3. DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
The database is where all of the data from the website will be stored. For this small project, we will use an sqlite3 database (django will create sqlite3 databases for us). Just fill in the information as I have above. If you are using a different database engine, then remember that Google is a friend. I will give a hint to MySQL users: Look at my previous post on setting up django with Passenger on Dream Host for instructions on how to set up MySQLdb.
# Local time zone for this installation. Choices can be found here: # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name # although not all choices may be available on all operating systems. # If running in a Windows environment this must be set to the same as your # system time zone. TIME_ZONE = 'America/New_York'
This one is very self explanatory. Just fill it in with your timezone. As I live in Pennsylvania, my timezone is Eastern Daylight Time/Eastern Standard Time, so I use America/New_York.
# Language code for this installation. All choices can be found here: # http://www.i18nguy.com/unicode/language-identifiers.html LANGUAGE_CODE = 'en-us'
Just leave this as is, unless you are of a different language or country. The codes can be found on the page referenced in the source code.
SITE_ID = 1This is used for django’s sites framework. You don’t have to worry about this, so just leave it be and ignore it.
# If you set this to False, Django will make some optimizations so as not # to load the internationalization machinery. USE_I18N = True
This is used for if you are addressing an international audience. For example, if your website will support multiple languages, this has to be True. You can change this to False if you wish, but I usually leave it as True.
# Absolute path to the directory that holds media. # Example: "/home/media/media.lawrence.com/" MEDIA_ROOT = '' # URL that handles the media served from MEDIA_ROOT. Make sure to use a # trailing slash if there is a path component (optional in other cases). # Examples: "http://media.lawrence.com", "http://example.com/media/" MEDIA_URL = '' # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a # trailing slash. # Examples: "http://foo.com/media/", "/media/". ADMIN_MEDIA_PREFIX = '/media/'
The MEDIA directory is a folder on the web server where you will store images, CSS files, JavaScript files, Flash animations, and anything else you want to use on the website. Typically, the admin site’s media files will also be stored in this MEDIA directory. As we are developing a very basic application, there is no need to touch any of this right now.
# Make this unique, and don't share it with anybody. SECRET_KEY = 'zf9(rfx6=mi0c+ouz^((%vi)xuqg795^j@!1*%%i^155_+i2e1'
Your key will be different, so don’t worry. This is used to help encode different bits of information such as passwords for security purposes. Just leave it be as django pseudo-randomly generates it when the project is created.
# List of callables that know how to import templates from various sources. TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.load_template_source', 'django.template.loaders.app_directories.load_template_source', # 'django.template.loaders.eggs.load_template_source', )
These are just some modules used by django to load the templates. More on templates later. For now, just leave this be.
MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', )
Middleware are used to intercept requests and responses and do some processing on them. Until you start into larger projects with django, you will not have to worry about changing items in this list.
ROOT_URLCONF = 'myblog.urls'This tells django where to look for your URL routing file. The URL routing file tells django what each URL points to. We’ll cover this when we start developing the views for the blog application.
TEMPLATE_DIRS = ( # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. )
The templates are the pages that the user will see. We’ll talk more on them when we cover views. For now, leave this variable unchanged but remember that it is here.
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'myblogs.blog', )
Finally, the end of the file! These are the applications that are installed on the site. django.contrib.auth handles user authentication (the admin site utilizes this). django.contrib.contenttypes handles the different types of data that can be stored on your site (such as posts and comments on your blog). django.contrib.sessions stores information about the current session (used by django.contrib.auth). django.contrib.sites is that sites framework I told you about that you don’t need to worry about. Finally, add ‘myblogs.blog’ onto the list and hit save.
And thus part one has come to an end. You have successfully created the project, application, and configured the project. The next part of this tutorial will be about models, so stay tuned!
Comments
Leave a comment Trackback