背景
为便于后续维护,前期对django创建等操作做一些规范化要求
实操
1、【项目创建】先创建一个项目,其中SSS可以项目架构相关,XXX则是项目名称
常用命令:
django startproject SSS_app_XXX
如果提示报错django命令不存在,则使用以下
django-admin startproject SSS_app_XXX
例子参考:
2、【代码上传】可以在允许的代码托管仓库,如GitHub、gitlab、gitee新建对应仓库并同步代码
mv SSS_app_XXX SSS-app-XXX
cd SSS-app-XXX
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://######.com/###/back-app-article.git
git push -u origin "master"
例子参考:
备注:django直接创建的项目名称中不能带‘-’故这里使用mv操作来中转
3、【虚拟环境】使用pycharm打开并新建虚拟环境,建议使用mac或linux系统里面安装pycharm开发,飞一般的感觉
pycharm首次打开:
新建虚拟环境:
pycharm左上角的文件——设置——python解释器——添加解释器——添加本地解释器——新建
虚拟环境新建完后,点击如图所示终端,看到下图圈出来部分说明虚拟环境设置成功(虚拟环境是隔离宿主机环境,以免因环境问题后续调试数据有误)
4、【.gitignore配置】项目结构下新增.gitignore文件,并push上传
参考模板:
# Created by .ignore support plugin (hsz.mobi)
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
################ CUSTOM SECTION ################
/.idea
/back*/settings
/back*/settings.py
/static
.session
/
5、配置依赖相关requirements.txt
由于是django项目且后续需要连接mysql,目前所需要的库为:
Django~=4.1.4
mysqlclient==2.1.1
备注:后续可以使用以下命令安装相关的库
python3 -m pip install -r requirements.txt
6、【创建应用】创建django应用app
python3 manage.py startapp XXX
例子参考:
7、【新增settings_base.py】
settings.py:含账号信息等,不上传云端;
settings_base.py:可以上传云端的配置文件,一般无隐私信息;
settings.py
from .settings_base import *
### settings
8、数据库配置
使用mysql数据库,settings.py里面新增
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_back_app_article',
'USER': 'root',
'PASSWORD': ###,
'HOST': '127.0.0.1',
'PORT': 3306,
'OPTIONS': {
'charset': 'utf8mb4',
'init_command': '''
SET default_storage_engine=InnoDB;
SET sql_mode='STRICT_TRANS_TABLES';
''',
},
}
}
备注:记得先去本地数据库里面创建django_back_app_article数据库后,再连接测试!
9、【应用注册】
将新建的应用加到settings_base.py中
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'article'
]
10、【运行】
命令:
python manage.py runserver
日志:
页面效果:
以上基础ok后,可以开心的去开发了。