前提: 在安装Django前,你需要安装python开发环境。
Python下有许多款不同的 Web 框架。Django是重量级选手中最有代表性的一位。Django是一个开放源代码的Web应用框架,由Python写成。Django采用了MVC的软件设计模式。
Django 下载地址: https://www.djangoproject.com/download/
Mac OS 使用pip安装:
pip install Django==2.2
查看Django 版本信息,来确认是否安装成功
django-admin --version
Django Admin
安装Django 之后,可以使用Django管理工具 django-admin来创建一个项目。
我们先看一下django-admin命令:
$ django-admin
Type 'django-admin help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
...
创建项目
使用Django-admin来创建test项目:
django-admin startproject helloworld
目录结构如下:
|-- HelloWorld
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| `-- wsgi.py
`-- manage.py
然后我们启动服务器:
python3 manage.py runserver 0.0.0.0:8000
0.0.0.0 让其它电脑可连接到开发服务器,8000 为端口号。如果不说明,那么端口号默认为 8000。
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 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.
April 07, 2019 - 13:07:24
Django version 2.2, using settings 'helloworld.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
上面的日志代表,服务器已经启动,我们只要打开浏览器,输入本机地址,即可访问网站。输出结果如下:

此时,其他机器如果要访问这台电脑的地址,会出现错误。需要在项目中的setting.py中进行如下修改:
ALLOWED_HOSTS = ['*']
重新运行程序,此时其他机器就可以进行访问了。
网友评论