本来一般的python server都用flask的,最近接触django项目,所以开始学习django.
安装
pip安装
pip install Django
helloWorld
django-admin.py startproject HelloWorld
wujingcideMacBook-Pro:HelloWorld wujingci$ tree
.
├── HelloWorld
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
分析manage.py
cat manage.py
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "HelloWorld.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
看一下execute_from_command_line
from django.core.management import execute_from_command_line
目录在
/usr/local/lib/python3.6/site-packages/django/core/management/init.py
一个类ManagementUtility,直接看execute
有几个subcommand,有个help,我们跑跑看
python3.6 manage.py help
Type 'manage.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[auth]
changepassword
createsuperuser
[contenttypes]
remove_stale_contenttypes
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
看到了一个runserver,我们试试看
python3.6 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 14, 2018 - 13:20:26
Django version 2.0.1, using settings 'HelloWorld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[14/Jan/2018 13:20:29] "GET / HTTP/1.1" 200 16559
HelloWorld已经跑起来了,下节我们再看其他文件。