Django--Online Album Management System (1)

Table of contents

1. Testimonials

2. Finished product

3. Preliminary preparation

4. Project basic configuration

4.1. File configuration in the AlbumManage folder

4.2. File configuration in the ablumapp folder



1. Testimonials

This is my first time writing a blog, sorry for the bad writing.

Every time after learning the technology, I want to write a few blogs to record, but I am lazy and keep procrastinating. Now I am bored, so I will start my first blog!

This blog of mine is mainly aimed at friends who are beginners of Django. If you are honored to have a boss who sees my blog and finds any problems, please advise, thank you very much! !

My album management system includes basic functions such as adding, browsing, deleting, and viewing. Without further ado, let's start!

2. Finished product

3. Preliminary preparation

These specific packages need to be installed! !

pip install django==2.2.*
pip install  mysqlclient  
pip install Pillow  # 图像处理标准库

4. Project basic configuration

At first, open this window by holding shift + right-clicking on the directory you need to save. (Of course, you can also use the pycharm terminal directly, this step can be skipped.)

After entering, enter the code to create the project, then enter the project folder, and create the application.

Open the project with pycharm, create two folders, static is used to store static files, and templates is used to store web templates.


4.1. File configuration in the AlbumManage folder

 Open the settings.py file under the AlbumManage folder, here are mainly the settings of this project:

1. Find and set it to ALLOWED_HOSTS=['*'], which means unified allocation, allowing the use of ip addresses, so the machine can be connected.

2. Add application name

3. Set the webpage template path file, BASE_DIR will directly locate the project, just fill in the folder name after the comma.

4. For the information configuration of the database, the content can be copied directly, and some information can be modified. The project uses the MySQL database, remember to create the "myalbum" database.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'myalbum',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

 5. Static file path configuration, the same as point 3. (Finish)

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

Then open the urls.py file, which is mainly used to configure the routing of your application. When your project is running, it will point to the module file in the later part according to the content in the front part of the path. If you are still confused, you may understand when you see the route to add the corresponding function of the application later.

Add the include library, and then add the module file path pointed to by the route (urls file under ablumapp)

4.2. File configuration in the ablumapp folder


The files in the AlbumManage folder are configured, and the next step is to write the application software created before.

When it was first created, there was no such file. You need to create it yourself. The name is up to you, but it must be the same as the name of the previously configured application route, otherwise the system will not find it.

This folder only uses three files in this project: urls.py (routing configuration corresponding to the application function), views.py (code for function implementation), and modes.py (database model code).

1. Add the following code in the urls.py file, keep it complete, otherwise it may report an error, test it yourself!

from django.urls import path
from . import views
urlpatterns = [
    #应用程序中对应功能的路由
]

2. Define the model class in models.py, without explaining it in detail again, you can go to the Django official website to understand it yourself.

from datetime import datetime

from django.db import models

# Create your models here.
class Album(models.Model):  # 类名可不用与数据库名字一致

    # 数据库列的定义
    title = models.CharField(max_length=32)
    type = models.CharField(max_length=32)
    add_time = models.DateTimeField(default=datetime.now)

    def __str__(self):
        return self.name + ":" + self.phone


Model model under popular science:

The model is the single, authoritative source of information about your data. It contains the necessary fields and behaviors for the data you store.

Typically, each model corresponds to a unique table in the database.

  • Each model is a Python subclass of django.db.models.Model.
  • Each property of the model is represented as a field in the database.
  • Django provides a set of automatically generated APIs for database access;
  • This greatly reduces the workload of developers and does not need to face ineffectiveness caused by database changes

Why use the model model?

Model is an important part of the MVC framework, which is mainly responsible for the part of the program used to process data logic. Usually model objects are responsible for accessing data in the database

It realizes the decoupling of the data model and the database, that is, the design of the data model does not need to depend on a specific database, and the database can be easily replaced through simple configuration

 Generate migration files:

python manage.py makemigrations

Execute the migration:

python manage.py migrate

After completion, the database will have a corresponding data table.

This is all the preparatory work before writing the function, and I will publish another chapter later, thank you everyone!

This is the first time I write a blog. If you don’t understand something or have any questions, you can private message me or leave a message in the comment area.

 OK, I'm done!

Address of the second article:

Django--Online Album Management System (2)_Orange Hahaha~'s Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/qq_56966336/article/details/122333859