一、创建一个django项目
1. 创建项目
django-admin startproject projectname
2. 运行项目
python manage.py runserver 127.0.0.1:8000
二、定义模型
1. 创建app
django-admin startapp TestModel
2. 运行项目
python manage.py runserver 127.0.0.1:8000
三、定义模型
创建app
django-admin startapp TestModel
四. 运行项目
python manage.py runserver 127.0.0.1:8000
五、上传
1. 创建app
django-admin startapp TestModel
2. 运行项目
python manage.py runserver 127.0.0.1:8000
六、建立远程仓库并提交代码
1. 创建requirement.txt
环境以及安装包十分重要,需要保存或者建立适合的环境
(1)导入:进入虚拟环境,然后进入我们需要的目录,把安装包导入到requirements.txt 文件中
pip freeze > ./requirements.txt
(2)复原:接收到requirements.txt文件后,把所有的安装包安装到我们的环境中
pip install -r requirements.txt
2. 创建.gitignore文件
创建git的忽略名单,在项目代码中,有些文件不能上传,如密码文件、数据库文件、核心配置文件、临时文件。 安装.gitignore插件:
进入setting-->Plugins-->Browse Repositories-->安装gitignore
在项目根目录下新建.gitignore文件:
.idea
settings.py
db.sqlite3
mysite/__pycache__/
3.特殊文件处理
对于settings.py文件中,SECRET_KEY,数据库的IP/port、用户名和密码,邮件发送端的用户名和密码,这些都是绝对不能泄露的。
复制settings文件,并重命名为settings.example.py文件,放在同一目录里,把敏感信息、密码等修改或删除
4.添加说明文件和许可文件
README.md:markdown格式,写说明
进入setting-->Plugins-->Browse Repositories-->安装markdown support
setting-->Editor-->File Types-->Markdown-->看文件后缀
LICENSE: 许可文件 对于许可文件LICENSE,如果你暂时不想公开授权,或者不知道用哪种授权,可以暂时不提供。
下面是一个APACHE2.0授权的范例:
mysite - User login and register system
Copyright 2017- www.liujiangblog.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
5.上传代码
点击VCS-->import into version control-->share Project on github
6.使用github仓库中的源码
如果你不是从教程的开始一步步地实现整个项目,而是直接使用从Github上copy下来的整个源码,那么需要做一些额外的工作,比如:
- 创建虚拟环境
- 使用pip安装第三方依赖
- 修改settings.example.py文件为settings.py
- 运行migrate命令,创建数据库和数据表
- 运行python manage.py runserver启动服务器
而在Pycharm中运行服务器的话,可能还需要做一些额外的工作,比如:
- 配置解释器
- 配置启动参数