官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 容城县| 陆河县| 佳木斯市| 襄樊市| 霸州市| 桐庐县| 金坛市| 额尔古纳市| 温泉县| 伊金霍洛旗| 凤山县| 襄城县| 永靖县| 棋牌| 九龙城区| 鄂伦春自治旗| 青冈县| 河津市| 肇庆市| 平南县| 县级市| 灵丘县| 西乌珠穆沁旗| 荔浦县| 兴文县| 安宁市| 腾冲县| 长乐市| 乐至县| 龙泉市| 买车| 岳池县| 宁陵县| 华坪县| 平罗县| 巴楚县| 建德市| 平江县| 文水县| 漠河县| 朝阳区|