超详细创建Django项目并存在Gitee仓库中
已做免密是配置,复制仓库ssh的URL,若未做免密配置,请选择https的URL
本文是已做免密设置,与未作免密配置的差别就在于连接仓库的时候是否验证而已
1.创建全局豆瓣镜像(提高下载速度)
windows环境下:
Administrator@DESKTOP-VJ0GDUL MINGW64 ~ $ cd
Administrator@DESKTOP-VJ0GDUL MINGW64 ~ $ mkdir pip
Administrator@DESKTOP-VJ0GDUL MINGW64 ~ $ cd pip
Administrator@DESKTOP-VJ0GDUL MINGW64 ~/pip $ vim pip.ini 写 [global] index-url=https://pypi.doubanio.com/simple
linux环境下: Administrator@DESKTOP-VJ0GDUL MINGW64 ~ $ cd
Administrator@DESKTOP-VJ0GDUL MINGW64 ~ $ mkdir .pip
Administrator@DESKTOP-VJ0GDUL MINGW64 ~ $ cd .pip
Administrator@DESKTOP-VJ0GDUL MINGW64 ~/pip $ vim pip.conf 写 [global] index-url=https://pypi.doubanio.com/simple
2.安装django Administrator@DESKTOP-VJ0GDUL MINGW64 /d $ pip install django==2.2.9 Looking in indexes: https://pypi.doubanio.com/simple Collecting django==2.2.9 Downloading https://pypi.doubanio.com/packages/cb/c9/ef1e25bdd092749dae74c95c2707dff892fde36e4053c4a2354b2303be10/Django-2.2.9-py3-none-any.whl (7.5MB) Requirement already satisfied: sqlparse in c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages (from django==2.2.9) (0.3.0) Requirement already satisfied: pytz in c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages (from django==2.2.9) (2019.3) Installing collected packages: django Found existing installation: Django 2.1.14 Uninstalling Django-2.1.14: Successfully uninstalled Django-2.1.14 Successfully installed django-2.2.9 You are using pip version 19.0.3, however version 19.3.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
3.创建项目 // 创建django项目 Administrator@DESKTOP-VJ0GDUL MINGW64 /d $ django-admin startproject djangocase
Administrator@DESKTOP-VJ0GDUL MINGW64 /d $ cd djangocase/
// 创建仓库 Administrator@DESKTOP-VJ0GDUL MINGW64 /d/djangocase $ git init Initialized empty Git repository in D:/djangocase/.git/
// 设置忽略文件(写入vevn,.idea和http://gitignore.io/这个网址生成的(windows,macOS,Linux,python,Pycharm)) Administrator@DESKTOP-VJ0GDUL MINGW64 /d/djangocase (master) $ vim .gitignore
// 写入依赖项 // Django==2.2.9,django-redis,mysqlclient等 Administrator@DESKTOP-VJ0GDUL MINGW64 /d/djangocase (master) $ vim requirements.txt
// 将文件添加到缓存区 Administrator@DESKTOP-VJ0GDUL MINGW64 /d/djangocase (master) git status On branch master
No commits yet
Changes to be committed: (use "git rm --cached
new file: .gitignore new file: djangocase/init.py new file: djangocase/settings.py new file: djangocase/urls.py new file: djangocase/wsgi.py new file: manage.py new file: requirements.txt
// 将缓存区中的内容全部提交到git本地仓库中 Administrator@DESKTOP-VJ0GDUL MINGW64 /d/djangocase (master) $ git commit -m '项目初始版本' [master (root-commit) a4e363f] 项目初始版本 7 files changed, 450 insertions(+) create mode 100644 .gitignore create mode 100644 djangocase/init.py create mode 100644 djangocase/settings.py create mode 100644 djangocase/urls.py create mode 100644 djangocase/wsgi.py create mode 100644 manage.py create mode 100644 requirements.txt
// 查看提交日志 Administrator@DESKTOP-VJ0GDUL MINGW64 /d/djangocase (master) $ git log commit a4e363f1ab001cb89f35c02058d7097ef4468e66 (HEAD -> master) Author: Your Name [email protected] Date: Mon Jan 6 09:56:10 2020 +0800
项目初始版本
// 设置版本号 Administrator@DESKTOP-VJ0GDUL MINGW64 /d/djangocase (master) $ git tag v0.1
Administrator@DESKTOP-VJ0GDUL MINGW64 /d/djangocase (master) $ git log commit a4e363f1ab001cb89f35c02058d7097ef4468e66 (HEAD -> master, tag: v0.1) Author: Your Name [email protected] Date: Mon Jan 6 09:56:10 2020 +0800
项目初始版本
// 关联远程仓库 Administrator@DESKTOP-VJ0GDUL MINGW64 /d/djangocase (master) git remote -v origin [email protected]:wang_ba_tian/djangocase.git (fetch) origin [email protected]:wang_ba_tian/djangocase.git (push)
// 允许忽略不相关文件(当仓库存在和本地不同的文件的时候) Administrator@DESKTOP-VJ0GDUL MINGW64 /d/djangocase (master) git push -u origin master Enumerating objects: 10, done. Counting objects: 100% (10/10), done. Delta compression using up to 8 threads Compressing objects: 100% (9/9), done. Writing objects: 100% (10/10), 4.82 KiB | 1.20 MiB/s, done. Total 10 (delta 0), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-3.8] To gitee.com:wang_ba_tian/djangocase.git
- [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.
Administrator@DESKTOP-VJ0GDUL MINGW64 /d/djangocase (master) $ git log commit a4e363f1ab001cb89f35c02058d7097ef4468e66 (HEAD -> master, tag: v0.1, origin/master) Author: Your Name [email protected] Date: Mon Jan 6 09:56:10 2020 +0800
项目初始版本
Administrator@DESKTOP-VJ0GDUL MINGW64 /d/djangocase (master) $
远程仓库创建完毕,本地文件可以删除
4.用Pycharm远程clone项目
在Pycharm中git下仓库(url:[email protected]:wang_ba_tian/djangocase.git)
在file下的setting创建虚拟环境
点击选择add
创建成功后在Pycharm下方的Termial中下载依赖项
(venv) E:\PythonProject\djangocase>pip install -r requirements.txt 更新requirements.txt中的依赖项 (venv) E:\PythonProject\djangocase>pip freeze > requirements.txt
修改项目下的urls
...
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', // 配置模板路径 'DIRS': [os.path.join(BASE_DIR, 'templates')], .... }, ] ... //配置数据库 DATABASES = { 'default': { // 数据库类型 'ENGINE': 'django.db.backends.mysql', // 数据库名字 'NAME': 'djangocase', // 数据库地址 'HOST': '本地或者服务器地址', // 端口 'POST': 3306, // 数据库用户名 'USER': '用户名', // 密码 'PASSWORD': '密码', // 编码方式 'CHARSET': 'utf8', // 设置时区 'TIME_ZONE': 'Asia/Chongqing' } } ... //配置时区 TIME_ZONE = 'Asia/Chongqing' ... // 配置静态资源路径 STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_URL = '/static/'
至此,项目创建完成,可以开始你的项目编写了.
题外话: 工作时,项目一般情况都是直接git clone公司的,不用自己去创建.