1 Template and static file Settings

1.1 Downloading a Template and static File

  • Use PyCharm to open the sandboxMP project created in the previous section and create two new folders under the sandboxMP project directory: static and Media;
  • Download the template and the static file (download address: pan.baidu.com/s/1LbIv2uXw… Extraction code: CN4T)
  • Copy the files in the media, static, and Templates directories of the downloaded file to the corresponding directory of the project

Note: If the static files in baidu web disk cannot be downloaded, you can use git to clone the tag version v1.02 that corresponds to this section. This version contains all the Settings in this section.

Install git and run the following clone command from git bash
After cloning, you can either use this version directly, or just copy the files in the Media, static, and Templates directories of this version into the project you created in the previous section
git clone- branch v1.02 [email protected]: RobbieHan/sandboxMP. GitCopy the code

After completing the above Settings, the project file directory structure is as follows:

sandboxMP/
    |-- media/                    # File upload directory
    |-- sandboxMP/
        |-- __init__.py           Empty file, the python package declaration file
        |-- settings.py           # Project configuration
        |-- urls.py               Item routing, used for URL declaration
        |-- wsgi.py               Wsgi compatible interface
    |-- static                    # Static file directory
    |-- templates/                # Use to store template files (HTML)
            |-- base-layer.html       The popover page in the project needs to inherit the base template page
            |-- base-left.html        # Left navigation
            |-- base-static.html      Static file page with global CSS and javascripts
            |-- head-footer.html      # Header navigation and bottom copyright information
            |-- index.html            # initial page, based on which the project will be laid out several times
            |-- page404.html          # 404 page
    |-- db.sqlite3                Sqlite3 database file
    |-- manage.py                 Command line tool for running projects, creating apps, etc
    
Copy the code

Template inheritance:

Base-static. HTML → head-footer. HTML → base-left. HTML → index.htmlCopy the code

1.2 Template and Static File Configuration

Example Set the template access path

We created templates to house the template page. To make Django find templates, we need to configure templates again with settings.py, adding ‘DIRS’: [os.path.join(BASE_DIR, ‘templates’)] specifies a list of directories to look for templates.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates'.'DIRS': [os.path.join(BASE_DIR, 'templates')].'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug'.'django.template.context_processors.request'.'django.contrib.auth.context_processors.auth'.'django.contrib.messages.context_processors.messages',],},},]Copy the code

Set the static file access path

Site used in pictures, these are collectively referred to as static JavaScript and CSS files, provides the Django Django. Contrib. Staticfile to help us to manage them.

We created a static in the project root directory directory to store static files, if you want to use it need in sandboxMP/sandboxMP/Settings. Py to add the following configuration:

STATIC_URL = '/static/'  It was added by default when you created your Django project

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
Copy the code

2 Change the language and time zone

Open sandboxMP sandboxMP/Settings. Py files, find LANGUAGE_CODE modified as follows:

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False
Copy the code

The latest and most complete document is published in knowledge planet, you can search the public account “knowledge planet” through wechat, directly reply to “52824366” to get access to this section of the document corresponding source version: github.com/RobbieHan/s…

Very welcome interested friends, to my Github or nuggets guest, spare time to give a “like” or “Star,” gift rose hand left lingering incense document supporting project address: github.com/RobbieHan/s… Lightweight office management system project open source address: github.com/RobbieHan/g…