Django从理论到实战(part3)--创建一个Django项目

学习笔记,仅供参考

本系列Blog以应用为主,理论基础部分我在后端专栏Django系列博客已经写过了,如果有些需要补充的知识点,我会在这个系列中,尽量详细的写一下。




创建一个Django项目


设置虚拟环境


  • 进入虚拟环境
workon mymkvir
  • 查看虚拟环境下的包
pip list

#输出
Package    Version
---------- -------
Django     2.2.13
pip        20.1.1
pytz       2020.1
setuptools 47.3.1
sqlparse   0.3.1
wheel      0.34.2

创建项目


  • 创建Django项目(newwebsite1)

进入要存放项目的目录:

cd F:\MyStudio\PythonStudio\goatbishop.project01\Django

创建一个项目:

django-admin startproject [项目名称]
#比如
django-admin startproject newwebsite1
  • 进入新创建的项目(newwebsite1)

进入项目文件:

cd newwebsite1

查看项目文件夹下的文件:

dir

#输出
2020/07/01  12:16    <DIR>          .
2020/07/01  12:16    <DIR>          ..
2020/07/01  12:16               652 manage.py
2020/07/01  12:16    <DIR>          newwebsite1
               1 个文件            652 字节
               3 个目录 26,557,247,488 可用字节

  • 运行该项目
python manage.py runserver
#输出
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.
July 01, 2020 - 12:34:28
Django version 2.2.13, using settings 'newwebsite1.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

默认监听8000端口。


  • 在浏览器中向http://127.0.0.1:8000/发起请求


  • 指定端口号为5000开启服务
python manage.py runserver 5000
#输出
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.
July 01, 2020 - 12:39:53
Django version 2.2.13, using settings 'newwebsite1.settings'
Starting development server at http://127.0.0.1:5000/
Quit the server with CTRL-BREAK.

  • 更改ip地址

如果我们网站的IP地址为127.0.0.1,那么只有我们自己的电脑可以访问这个项目,别人是访问不到的,如果我想要别人(同一个局域网下)也可以访问,就需要指定IP地址为0.0.0.0

python manage.py runserver 0.0.0.0:8000

在项目文件夹下的settings.py中修改允许访问的域名ALLOWED_HOSTS:

ALLOWED_HOSTS = ['*']
#或者是别人的ip地址

这样别人的电脑就可以访问我们的项目了(注意要关闭防火墙)。


查看manage.py中还有哪些子命令


python manage.py help
#输出
[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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章