Django安装及运行第一个web项目

以需求来驱动学习是职场学习最佳模式

安装django及验证

D:\PythonPrograms\InterfaceWithPython>pip install django
Collecting django
  Downloading https://files.pythonhosted.org/packages/b2/79/df0ffea7bf1e02c073c2633702c90f4384645c40a1dd09a308e02ef0c817/Django-2.2.6-py3-none-any.whl (7.5MB)
    100% |████████████████████████████████| 7.5MB 1.4MB/s
Collecting sqlparse (from django)
  Downloading https://files.pythonhosted.org/packages/ef/53/900f7d2a54557c6a37886585a91336520e5539e3ae2423ff1102daf4f3a7/sqlparse-0.3.0-py2.py3-none-any.whl
Collecting pytz (from django)
  Downloading https://files.pythonhosted.org/packages/e7/f9/f0b53f88060247251bf481fa6ea62cd0d25bf1b11a87888e53ce5b7c8ad2/pytz-2019.3-py2.py3-none-any.whl (509kB)
    100% |████████████████████████████████| 512kB 839kB/s
Installing collected packages: sqlparse, pytz, django
Successfully installed django-2.2.6 pytz-2019.3 sqlparse-0.3.0

D:\PythonPrograms\InterfaceWithPython>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
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must e
ither define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).

查看版本

(venv) D:\InterfaceProgram>pip show django
Name: Django
Version: 2.2.6
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: [email protected]
License: BSD
Location: c:\python37\lib\site-packages
Requires: pytz, sqlparse
Required-by:

创建Django项目

使用django-admin命令创建django项目,执行命令为:

(venv) D:\InterfaceProgram>django-admin startproject guest

该命令会自动建一个名为guest的项目,项目结构为

guest/
|----guest/
|----|----__init__.py
|----|----settings.py
|----|----urls.py
|----|----wsgi.py
|--------manage.py

然后进入该项目的根目录下,执行manage.py文件,Manage.py所提供的命令与django-admin是一致的。如果想进一步了解django-admin和manage.py的区别可以访问官方文档

(venv) D:\InterfaceProgram\guest>python manage.py

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

创建Django应用

(venv) D:\InterfaceProgram\guest>django-admin startapp sign
sign/
|----migrations/  # 用于记录models中数据的变更
|----admin.py  # 映射models中的数据到Django自带的admin后台
|----apps.py  # 用于应用程序的配置,在新的Django版本中新增文件
|----models.py  # Django的模型文件,创建应用程序数据表模型(对应数据库的相关操作)
|----tests.py  # Django测试用例
|----views.py  # Django的试图文件,控制向前端页面显示的内容

然后看一下工程结构为:
在这里插入图片描述
创建了名为sign的django应用,目录结构为


启动web项目

(venv) D:\InterfaceProgram\guest>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.
October 18, 2019 - 15:00:52
Django version 2.2.6, using settings 'guest.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[18/Oct/2019 15:03:19] "GET / HTTP/1.1" 200 16348
[18/Oct/2019 15:03:20] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[18/Oct/2019 15:03:20] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[18/Oct/2019 15:03:20] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
[18/Oct/2019 15:03:20] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
Not Found: /favicon.ico
[18/Oct/2019 15:03:20] "GET /favicon.ico HTTP/1.1" 404 1971

然后用浏览器打开http://127.0.0.1:8000/,如下图所示:
在这里插入图片描述
还可以发布到别的端口上,如下命令所示:

(venv) D:\InterfaceProgram\guest>python manage.py runserver 127.0.0.1:8001
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.
October 18, 2019 - 15:10:24
Django version 2.2.6, using settings 'guest.settings'
Starting development server at http://127.0.0.1:8001/
Quit the server with CTRL-BREAK.

Django的工作逻辑如下图所示

在这里插入图片描述

发布了162 篇原创文章 · 获赞 42 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/dawei_yang000000/article/details/102609183