将终端切换到项目目录:
1.建立虚拟环境 : python3 -m venv myproject_env
chasondeiMac:myproject chason$ python3 -m venv myproject_env
chasondeiMac:myproject chason$
2.激活虚拟环境 :source myproject_env/bin/activate
chasondeiMac:myproject chason$ source myproject_env/bin/activate
(myproject_env) chasondeiMac:myproject chason$
3.安装Django : pip3 install Django
(myproject_env) chasondeiMac:myproject chason$ pip3 install Django
Collecting Django
Using cached Django-1.11.5-py2.py3-none-any.whl
Collecting pytz (from Django)
Using cached pytz-2017.2-py2.py3-none-any.whl
Installing collected packages: pytz, Django
Successfully installed Django-1.11.5 pytz-2017.2
(myproject_env) chasondeiMac:myproject chason$
4.在Django中创建项目 (注意最后面有一个点): django-admin.py startproject myproject .
(myproject_env) chasondeiMac:myproject chason$ django-admin.py startproject myproject .
(myproject_env) chasondeiMac:myproject chason$
5.创建数据库 : python3 manage.py migrate
(myproject_env) chasondeiMac:myproject chason$ python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
(myproject_env) chasondeiMac:myproject chason$
6.查看项目是否正确创建 : python3 manage.py runserver
(myproject_env) chasondeiMac:myproject chason$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
September 06, 2017 - 08:04:39
Django version 1.11.5, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
7.在浏览器中输入http://127.0.0.1:8000/,若出现如下页面说明一切正常。
若出现错误信息”That port is already in use”(指定端口已被占用),请执行命令python3 manage.py runserver 8001
,让Django使用另一个端口。
8.创建应用程序:python3 manage.py startapp myprojects
(myproject_env) chasondeiMac:myproject chason$ python3 manage.py startapp myprojects
(myproject_env) chasondeiMac:myproject chason$
9.定义模型
在models.py文件中定义模型。
10.激活模型
打开settings.py在INSTALLED_APPS中添加上前面创建的应用程序
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#我的应用程序
'myprojects'
]
接下来,需要让Django修改数据库,使其能够存储与模型相关的信息,为此,在终端执行命令:python3 manage.py makemigrations myprojects
然后,再执行命令:python3 manage.py migrate
注意:每次修改models.py文件后都必须执行以上两条命令。
11.创建超级用户:python3 manage.py createsuperuser
(myproject_env) chasondeiMac:myproject chason$ python3 manage.py createsuperuser
Username (leave blank to use 'chason'): myproject_admin
Email address:
Password:
Password (again):
Superuser created successfully.
(myproject_env) chasondeiMac:myproject chason$
12.向管理网站注册模型
在admin.py文件中添加admin.site.register(模型名字),之前别忘了导包。