Use Django to build the first day of blog notes-environment preparation

Use Django to build a blog without separation of front and back ends

1. Environment construction

I installed python3.7.3, MySQL5.7, Django2.2 in advance, programming using pycharm

1. Create a python virtual environment
1.1 Install virtual environment dependencies

pip install virtualenvwrapper-win

1.2 Create a virtual environment named Myblog, the default storage location of the virtual environment under Windows: C:\Users\username\Envs, this path is required when creating a project later

mkvirtualenv  -p C:\learn\Anaconda3\python.exe  Myblog

2. Install django2.2.0
2.1 and enter the virtual environment

workon Myblog

2.2 Use pip to install, switch to Douban source

pip intsall django==2.2.0 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

Use pip list to viewinsert image description here

2. Create a django project

Open pycharm, click Create New Project, the steps are shown in the figure
insert image description here

insert image description here

3. Project structure design

1. Create project applications account, album, article and interflow in the project, and create a routing file urls.py in each project application; finally create folders media, static and templates in the project (when creating the project, already It is automatically created for us, without us manually creating the folder ourselves). Use pycharm to create an application, the steps are as shown in the figure:
After clicking the tool at the top, click Run manage.py Task and
insert image description here
enter startapp account in the console below
insert image description here
to create urls.py
insert image description here

The three application creation steps of album, article and interflow are the same as the account application creation method above. In order to facilitate application management, we put the four applications in a folder named app:
insert image description here
insert image description here

Move the application into the app file.
insert image description here
The folders media and static are created in the same way as the app. The final file directory is shown in the figure below: The
insert image description here
description of the newly created project application and folder is as follows:
(1) The project application account realizes user registration, login and user ( Blogger) profile information page, the custom model MyUser inherits the built-in model User, and new fields are added on the basis of the built-in model User to improve user information. The blogger profile information page of the model MyUser provides data support, and the model MyUser needs to be associated with other models.

(2) The project uses album to realize the picture wall function, and the picture wall of each user (blogger) can only display the picture information uploaded by itself. The model AlbumInfo is used to store the picture information of the picture wall. It has a foreign key field association model MyUser, and forms a one-to-many data relationship with the model MyUser.

(3) The project uses article to realize the article management of users (bloggers). Each article is equipped with classification tags, text content and comment schedule. The three correspond to the models AriclTag, Articlelnfo and Comment respectively, and the data relationship between each model is explained As follows: The model ArticleTag has a foreign key field association model MyUser, which forms a one-to-many data relationship with the model MyUser, and the model Ariclelnfo not only forms a one-to-many data relationship with the model MyYUser, but also forms a many-to-many relationship with the model ArticleTag data relationship.

The model Comment only forms a one-to-many data relationship with the model ArticleInfo.

(4) The project uses nero to implement the message board function of the blog. The model Board stores message board information. It has a foreign key field association model MyUser, which forms a one-to-many data relationship with the model MyUser, thereby distinguishing each user (blogger) message board content.
(5) The media resource folder media stores resource files such as article pictures uploaded by users (bloggers), pictures on the picture wall, and user (blogger) avatars. Such resource files change frequently, so they are different from static resources. storage path.

(6) Static resource folder static stores static resources such as CSS style files of web pages, JavaScript script files, and web page images. If multiple project applications are created in the project, and static resource folders are created separately in each project application, then when updating or modifying the layout of the web page, it is not conducive to future maintenance and management.

(7) The template folder templates stores template files. This project uses a total of 7 template files. The description of each template file is as follows:

  • base.html defines the common template file for the project.

  • album.html realizes the web page content of the picture wall.

  • article.html implements the article list page.

  • board.html implements the web page content of the message board.

  • detail.html implements the content page of the article body.

  • user.html implements user registration and login pages.

  • about.html implements the user (blogger) profile information page.

4. Function configuration

Write the project application account, album, article and interflow into the configuration attribute INSTALLED_APPS of the Django configuration file setting.py, and add the middleware LocaleMiddleware in the configuration attribute MIDDLEWARE, so that the Admin acquired system supports Chinese language. The configuration code is as follows:

# Myblog 的 setting.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app.account',
    'app.album',
    'app.article',
    'app.interflow'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    # 中间件LocaleMiddleware
    'django.middleware.locale.LocaleMiddleware'
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

The data storage of the project adopts MySQL database. We create blogdb in MySQL and set the data connection method in the configuration property DATABASES. The configuration code is as follows:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'blogdb',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '127.0.0.1',
        'POST': '3306',
    }
}

Finally, introduce the static folder static and the media resource folder media into the Django operating environment, and change the Django built-in user model User to the custom model MyUser of the project application account. The configuration code is as follows:

# 配置自定义用户模型 应用.模型
AUTH_USER_MODEL = "account.MyUser"

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]

# 设置媒体资源的保存路径
MEDIR_URL = '/media'
MEDIR_ROOT = os.path.join(BASE_DIR, "media")

Guess you like

Origin blog.csdn.net/qq_45723275/article/details/120458603