Book Image

Django Project Blueprints

By : Asad Jibran Ahmed
Book Image

Django Project Blueprints

By: Asad Jibran Ahmed

Overview of this book

Django is a high-level web framework that eases the creation of complex, database-driven websites. It emphasizes on the reusability and pluggability of components, rapid development, and the principle of don't repeat yourself. It lets you build high-performing, elegant web applications quickly. There are several Django tutorials available online, which take as many shortcuts as possible, but leave you wondering how you can adapt them to your own needs. This guide takes the opposite approach by demonstrating how to work around common problems and client requests, without skipping the important details. If you have built a few Django projects and are on the lookout for a guide to get you past the basics and to solve modern development tasks, this is your book. Seven unique projects will take you through the development process from scratch, leaving no stone unturned. In the first two projects, you will learn everything from adding ranking and voting capabilities to your App to building a multiuser blog platform with a unique twist. The third project tackles APIs with Django and walks us through building a Nagios-inspired infrastructure monitoring system. And that is just the start! The other projects deal with customizing the Django admin to create a CMS for your clients, translating your web applications to multiple languages, and using the Elasticsearch search server with Django to create a high performing e-commerce web site. The seventh chapter includes a surprise usage of Django, and we dive deep into the internals of Django to create something exciting! When you're done, you'll have consistent patterns and techniques that you can build on for many projects to come.
Table of Contents (15 chapters)
Django Project Blueprints
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Setting up our development environment


For this first chapter, I'll go into some details about setting up the development environment. For later chapters, I'll only be providing minimal instructions. For further details about how I setup the development environment and why, take a look at Appendix, Development Environment Setup Details and Debugging Techniques.

Let's start by creating the directory structure for our project, setting up the virtual environment and configuring some base Django settings that need to be set up in every project. Let's call our blogging platform BlueBlog.

Note

Detailed explanations of the steps you're about to see are given in Appendix, Development Environment Setup Details and Debugging Techniques. Please refer to that if you're unsure about why we're doing something or what a particular command does.

To start a new project, you need to first open up your terminal program. In Mac OS X, it is the built-in terminal. In Linux, the terminal is named separately for each distribution, but you should not have trouble finding it; try searching your program list for the word terminal and something relevant should show up. In Windows, the terminal program is called the command line. You'll need to start the relevant program depending on your operating system.

Note

If you are using the Windows operating system, you will need to slightly modify the commands shown in the book. Please refer to the Developing on Windows section of Appendix, Development Environment Setup Details and Debugging Techniques for details.

Open the relevant terminal program for your operating system and start by creating the directory structure for our project; cd (ing) into the root project directory using the commands shown below:

> mkdir –p blueblog
> cd blueblog

Next let's create the virtual environment, install Django, and start our project:

> pyvenv blueblogEnv
> source blueblogEnv/bin/activate
> pip install django
> django-admin.py startproject blueblog src

With that out of the way, we're ready to start developing our blogging platform.

Database settings

Open up the settings found at $PROJECT_DIR/src/blueblog/settings.py in your favorite editor and make sure that the DATABASES settings variable matches this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

In order to initialize the database file, run the following commands:

> cd src
> python manage.py migrate

Static files settings

The last step in setting up our development environment, is configuring the staticfiles contrib application. The staticfiles application provides a number of features that make it easy to manage the static files (css, images, JavaScript) of your projects. While our usage will be minimal, you should look at the Django documentation for staticfiles in further detail, since it is used quite heavily in most real world Django projects. You can find the documentation at https://docs.djangoproject.com/en/stable/howto/static-files/.

In order to set up the staticfiles application we have to configure a few settings in the settings.py file. First, make sure that django.contrib.staticfiles is added to the INSTALLED_APPS. Django should have done that by default.

Next, set STATIC_URL to whatever URL you want your static files to be served from. I usually leave this to the default value, /static/. This is the URL that Django will put in your templates when you use the static template tag to get the path to a static file.

A base template

Next let's setup a base template that all the other templates in our application will inherit from. I prefer to have templates that are used by more than one application of a project in a directory named templates in the project source folder. To set that up, add os.path.join(BASE_DIR, 'templates') to the DIRS array of the TEMPLATES config dictionary in the settings file, and then create a directory named templates in $PROJECT_ROOT/src. Next, using your favorite text editor, create a file named base.html in the new folder with the following content:

<html>
<head>
    <title>BlueBlog</title>
</head>
<body>
    {% block content %}
    {% endblock %}
</body>
</html>

Much like Python classes inheriting from other classes, Django templates can also inherit from other templates. And just like Python classes can have functions overridden by their subclasses, Django templates can also define blocks that children templates can override. Our base.html template provides one block for inheriting templates to override, called content.

The reason for using template inheritance is code reuse. We should put HTML that we want to be visible on every page of our site, such as headers, footers, copyright notices, meta tags, and so on, in the base template. Then, any template inheriting from it will automatically get all that common HTML included automatically, and we will only need to override the HTML code for the block we want to customize. You'll see this principal of creating and overriding blocks in base templates used throughout the projects in this book.