Develop a simple music player using the django framework.
Effect:
Table of contents
Create a urls.py file in the player application directory
The sub-application route is added to the main route
Configure static file directory
Environmental preparation
install django
pip install Django==3.0
create project
django-admin startproject mymp3
create application
python manage.py startapp player
register application
Modify settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'player'
]
configuration database
Set database configuration
In settings.py DATABASES
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mymp3',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': '3306'
}
}
Set pymysql library reference
Add at the top of the __init.py file in the project directory
import pymysql
pymysql.install_as_MySQLdb()
create database
Create database mymp3, select utf8mb4.
Create data table
Generate table migration file
python manage.py makemigrations
Execute table migration
python manage.py migrate
Configure time zone
Configure time zone: modify it to China Shanghai time zone
TIME_ZONE = 'UTC'
changed to
TIME_ZONE = 'Asia/Shanghai'
configuration language
Configuration language: modify it to Simplified Chinese
LANGUAGE_CODE = 'en-us'
changed to
LANGUAGE_CODE = 'zh-hans'
Configure sub-app routing
Create a urls.py file in the player application directory
The content of Urls.py is as follows
from django.urls import path
urlpatterns = [
# path('admin/', admin.site.urls),
]
The sub-application route is added to the main route
Route urls.py in the mypm3 project directory to introduce sub-application routes
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
# 增加路由 指向player urls
path(r'player/', include(('player.urls', 'player'))),
]
Create a project template
Create project template path
Create templates/application name in the mymp3 directory
As shown below:
Set project template path
Modify the TEMPLATES parameter in settings.py
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',
],
},
},
]
Note: the os library needs to be introduced
static file directory
Create static file directory
It is necessary to create the static files to be used such as css, js, images and other file directories first
Create static/application name/js|css|img directory under mymp3
As shown below
Configure static file directory
Configure the static path used in settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
Summarize
This article is mainly about the preliminary preparation of the development project, installing django, creating projects, applications, configuring database tables, templates, static file directory configuration, and then starting to develop project operations.