Project full text index
『Python-Django Personal home page 』 Open source project overview
CMD Command line create project
django-admin.exe startproject MyHome
add to Xadmin and DjangoUeditor
Create in project directory extra_apps Folder , Will download okay Xadmin and DjangoUeditor copying . The directory of the finally configured folder should be as follows .
Create other folders
Project application directory configuration
In the configuration file, declare the new file directory for the project .
import os
import sys
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))
Configure debug parameters
Enable the debugging function and network access restriction function .
DEBUG = True # This site will be used after the website is built False Can be closed
ALLOWED_HOSTS = ['*'] # It can be accessed by any domain name
Application information configuration
INSTALLED_APPS = [
...
'xadmin', # xadmin The main body
'crispy_forms', # Render table module
'reversion', # Provide data rollback function for model through version setting
'apps.Configuration', # Environmental Science & Data configuration
'apps.MyHomePage', # My home page
'apps.GameTool', # Game features
]
Front end template configuration
by Django Project add TEMPLATES Template settings , Direct replication can solve most problems that the front end cannot show , Guarantee Xadmin Background display , And front-end application calls .
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',
],
'libraries': {
'staticfiles': 'django.templatetags.static',
}
},
},
]
Data warehouse configuration
The data applied in the project is not used directly sqlite3 Default .
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Language and time zone configuration
Set up Django The project language is Chinese , The time zone is east 8th District Shanghai time .
# Setup language 、 The time zone
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False # Database storage usage time ,True Time will be saved as UTC Time for
Apply file directory configuration
Configure static file directory and media file directory , be used for API The interface calls the corresponding resource data information , And uploading data .
# Add static file path
STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR, 'static') Deploy the server to solve Admin backstage CSS The pattern disappears , perform python manage.py collectstatic
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
# Set the path where we upload the file
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
HTTPS To configure
If you need to deploy HTTPS Encryption protocol configuration
# SECURITY Security Settings - Support http It is recommended to start when
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
SECURE_SSL_REDIRECT = True # Will all not SSL Request permanent redirection to SSL
SESSION_COOKIE_SECURE = True # Only through https transmission cookie
CSRF_COOKIE_SECURE = True # Only through https transmission cookie
SECURE_HSTS_INCLUDE_SUBDOMAINS = True # Strictly require the use of https Protocol transfer
SECURE_HSTS_PRELOAD = True # HSTS by
SECURE_HSTS_SECONDS = 60
SECURE_CONTENT_TYPE_NOSNIFF = True # Prevent browsers from guessing the content type of the asset
from django.urls import path, re_path, include
import xadmin
from xadmin.plugins import xversion
xadmin.autodiscover() # xversion Automatic module registration requires version control Model
xversion.register_models()
from django.views.static import serve
from .settings import * # The configuration file Media File path
urlpatterns = [
path('xadmin/', xadmin.site.urls),
re_path('media/(?P<path>.*)', serve, {
"document_root": MEDIA_ROOT}),
re_path(r'^static/(?P<path>.*)$', serve, {
'document_root': STATIC_ROOT}, name='static'),
]
Remember the user name and password you set
python manage.py createsuperuser # Create a superuser , Follow the prompts to enter the user name 、 mailbox 、 password , be used for django Background management system login