本次 Python 版本 2.7.18,Django 1.11.29
参考:Django 的文档 https://yiyibooks.cn/xx/Django_1.11.6/intro/tutorial01.html

安装

pip3 install Django==3.0.6 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install django

  1. C:\xiaohui\python-u8>pip install django -i https://pypi.tuna.tsinghua.edu.cn/simple
  2. DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python
  3. 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
  4. Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
  5. Collecting django
  6. Downloading https://pypi.tuna.tsinghua.edu.cn/packages/49/49/178daa8725d29c475216259eb19e90b2aa0b8c0431af8c7e9b490ae6481d/Django-1.11.29-py2.py3-none-any.whl (6.9MB)
  7. |████████████████████████████████| 7.0MB 376kB/s
  8. Collecting pytz (from django)
  9. Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d3/e3/d9f046b5d1c94a3aeab15f1f867aa414f8ee9d196fae6865f1d6a0ee1a0b/pytz-2021.3-py2.py3-none-any.whl (503kB)
  10. |████████████████████████████████| 512kB 354kB/s
  11. Installing collected packages: pytz, django
  12. Successfully installed django-1.11.29 pytz-2021.3
  13. WARNING: You are using pip version 19.2.3, however version 20.3.4 is available.
  14. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
  15. C:\xiaohui\python-u8>
  16. C:\xiaohui\python-u8>pip list
  17. DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python
  18. 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
  19. Package Version
  20. ----------------------------- -------
  21. astroid 1.6.6
  22. autopep8 1.6.0
  23. backports.functools-lru-cache 1.6.4
  24. colorama 0.4.4
  25. configparser 4.0.2
  26. Django 1.11.29
  27. enum34 1.1.10
  28. futures 3.3.0
  29. isort 4.3.21
  30. lazy-object-proxy 1.6.0
  31. mccabe 0.6.1
  32. pip 19.2.3
  33. pycodestyle 2.8.0
  34. pycparser 2.21
  35. pylint 1.9.5
  36. pythonnet 2.5.2
  37. pytz 2021.3
  38. setuptools 41.2.0
  39. singledispatch 3.7.0
  40. six 1.16.0
  41. toml 0.10.2
  42. wrapt 1.13.3
  43. WARNING: You are using pip version 19.2.3, however version 20.3.4 is available.
  44. You should consider upgrading via the 'python -m pip install --upgrade pip' command.

检测安装

django-admin

  1. C:\xiaohui\python-u8>django-admin --version
  2. 1.11.29
  3. C:\xiaohui\python-u8>django-admin
  4. Type 'django-admin help <subcommand>' for help on a specific subcommand.
  5. Available subcommands:
  6. [django]
  7. check
  8. compilemessages
  9. createcachetable
  10. dbshell
  11. diffsettings
  12. dumpdata
  13. flush
  14. inspectdb
  15. loaddata
  16. makemessages
  17. makemigrations
  18. migrate
  19. runserver
  20. sendtestemail
  21. shell
  22. showmigrations
  23. sqlflush
  24. sqlmigrate
  25. sqlsequencereset
  26. squashmigrations
  27. startapp
  28. startproject
  29. test
  30. testserver
  31. 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 either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
  32. C:\xiaohui\python-u8>

或者:使用 python -m django —version
C:\xiaohui\python-u8\demo\web\helloworld>django-admin —version
1.11.29

C:\xiaohui\python-u8\demo\web\helloworld>python -m django —version
1.11.29

创建自己的项目

django-admin startproject helloworld

  1. C:\xiaohui\python-u8\demo\web>
  2. # 创建 helloworld 项目
  3. C:\xiaohui\python-u8\demo\web>django-admin startproject helloworld
  4. # 查看目录(其中 __init__.py 是我自己事先建立的,从时间也可以看出来)
  5. C:\xiaohui\python-u8\demo\web>dir
  6. 驱动器 C 中的卷没有标签。
  7. 卷的序列号是 8C93-6835
  8. C:\xiaohui\python-u8\demo\web 的目录
  9. 2022/02/09 14:23 <DIR> .
  10. 2022/02/09 14:23 <DIR> ..
  11. 2022/02/09 14:23 <DIR> helloworld
  12. 2022/02/09 13:49 0 __init__.py
  13. 1 个文件 0 字节
  14. 3 个目录 11,223,138,304 可用字节
  15. C:\xiaohui\python-u8\demo\web>cd helloworld
  16. C:\xiaohui\python-u8\demo\web\helloworld>dir
  17. 驱动器 C 中的卷没有标签。
  18. 卷的序列号是 8C93-6835
  19. C:\xiaohui\python-u8\demo\web\helloworld 的目录
  20. 2022/02/09 14:23 <DIR> .
  21. 2022/02/09 14:23 <DIR> ..
  22. 2022/02/09 14:23 <DIR> helloworld
  23. 2022/02/09 14:23 830 manage.py
  24. 1 个文件 830 字节
  25. 3 个目录 11,223,138,304 可用字节
  26. C:\xiaohui\python-u8\demo\web\helloworld>cd helloworld
  27. C:\xiaohui\python-u8\demo\web\helloworld\helloworld>dir
  28. 驱动器 C 中的卷没有标签。
  29. 卷的序列号是 8C93-6835
  30. C:\xiaohui\python-u8\demo\web\helloworld\helloworld 的目录
  31. 2022/02/09 14:23 <DIR> .
  32. 2022/02/09 14:23 <DIR> ..
  33. 2022/02/09 14:23 3,229 settings.py
  34. 2022/02/09 14:23 788 urls.py
  35. 2022/02/09 14:23 414 wsgi.py
  36. 2022/02/09 14:23 0 __init__.py
  37. 4 个文件 4,431 字节
  38. 2 个目录 11,223,134,208 可用字节
  39. C:\xiaohui\python-u8\demo\web\helloworld\helloworld>
  40. -----
  41. 菜鸟教程说明:
  42. $ tree
  43. .
  44. |-- HelloWorld
  45. | |-- __init__.py
  46. | |-- asgi.py
  47. | |-- settings.py
  48. | |-- urls.py
  49. | `-- wsgi.py
  50. `-- manage.py
  51. 目录说明:
  52. HelloWorld: 项目的容器。
  53. manage.py: 一个实用的命令行工具,可让你以各种方式与该 Django 项目进行交互。
  54. HelloWorld/__init__.py: 一个空文件,告诉 Python 该目录是一个 Python 包。
  55. HelloWorld/asgi.py: 一个 ASGI 兼容的 Web 服务器的入口,以便运行你的项目。
  56. HelloWorld/settings.py: Django 项目的设置/配置。
  57. HelloWorld/urls.py: Django 项目的 URL 声明; 一份由 Django 驱动的网站"目录"
  58. HelloWorld/wsgi.py: 一个 WSGI 兼容的 Web 服务器的入口,以便运行你的项目。

启动服务

python manage.py runserver 0.0.0.0:8000

  1. C:\xiaohui\python-u8\demo\web\helloworld>python manage.py runserver 0.0.0.0:8000
  2. Performing system checks...
  3. System check identified no issues (0 silenced).
  4. You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
  5. Run 'python manage.py migrate' to apply them.
  6. February 09, 2022 - 15:16:08
  7. Django version 1.11.29, using settings 'helloworld.settings'
  8. Starting development server at http://0.0.0.0:8000/
  9. Quit the server with CTRL-BREAK.
  10. [09/Feb/2022 15:17:02] "GET / HTTP/1.1" 200 1716

访问服务 localhost:8000

图片.png

实战

视图view与URL

先建立 views.py 视图

  1. #!/usr/bin/python
  2. # coding=utf-8
  3. from django.http import HttpResponse
  4. def sayHello(request):
  5. return HttpResponse("hello world")

修改原来 urls.py

  1. """helloworld URL Configuration
  2. The `urlpatterns` list routes URLs to views. For more information please see:
  3. https://docs.djangoproject.com/en/1.11/topics/http/urls/
  4. Examples:
  5. Function views
  6. 1. Add an import: from my_app import views
  7. 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
  8. Class-based views
  9. 1. Add an import: from other_app.views import Home
  10. 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
  11. Including another URLconf
  12. 1. Import the include() function: from django.conf.urls import url, include
  13. 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
  14. """
  15. from django.conf.urls import url
  16. from django.contrib import admin
  17. urlpatterns = [
  18. url(r'^admin/', admin.site.urls),
  19. ]
  20. ----------
  21. 修改为
  22. ----------
  23. """helloworld URL Configuration
  24. The `urlpatterns` list routes URLs to views. For more information please see:
  25. https://docs.djangoproject.com/en/1.11/topics/http/urls/
  26. Examples:
  27. Function views
  28. 1. Add an import: from my_app import views
  29. 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
  30. Class-based views
  31. 1. Add an import: from other_app.views import Home
  32. 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
  33. Including another URLconf
  34. 1. Import the include() function: from django.conf.urls import url, include
  35. 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
  36. """
  37. from django.conf.urls import url
  38. # from django.contrib import admin
  39. # urlpatterns = [
  40. # url(r'^admin/', admin.site.urls),
  41. # ]
  42. from . import views
  43. urlpatterns = [
  44. url(r'^$', views.sayHello),
  45. ]

注意,不同版本的 Django url、path 位置不同,当前的 Django 1.11.29 还没搞懂怎么使用 path

在项目下创建自己的应用

python manage.py startapp polls

  1. C:\xiaohui\python-u8\demo\web\helloworld>
  2. C:\xiaohui\python-u8\demo\web\helloworld>python manage.py startapp polls
  3. C:\xiaohui\python-u8\demo\web\helloworld>dir
  4. 驱动器 C 中的卷没有标签。
  5. 卷的序列号是 8C93-6835
  6. C:\xiaohui\python-u8\demo\web\helloworld 的目录
  7. 2022/02/09 17:15 <DIR> .
  8. 2022/02/09 17:15 <DIR> ..
  9. 2022/02/09 15:13 12,288 db.sqlite3
  10. 2022/02/09 16:49 <DIR> helloworld
  11. 2022/02/09 14:23 830 manage.py
  12. 2022/02/09 17:15 <DIR> polls
  13. 2 个文件 13,118 字节
  14. 4 个目录 11,204,857,856 可用字

需要进入到项目目录里(manage.py 文件所在的目录),然后运行 python manage.py startapp app_name

数据库部分 sqlite

python manage.py migrate

  1. C:\xiaohui\python-u8\demo\web\helloworld>python manage.py migrate
  2. Operations to perform:
  3. Apply all migrations: admin, auth, contenttypes, sessions
  4. Running migrations:
  5. Applying contenttypes.0001_initial... OK
  6. Applying auth.0001_initial... OK
  7. Applying admin.0001_initial... OK
  8. Applying admin.0002_logentry_remove_auto_add... OK
  9. Applying contenttypes.0002_remove_content_type_name... OK
  10. Applying auth.0002_alter_permission_name_max_length... OK
  11. Applying auth.0003_alter_user_email_max_length... OK
  12. Applying auth.0004_alter_user_username_opts... OK
  13. Applying auth.0005_alter_user_last_login_null... OK
  14. Applying auth.0006_require_contenttypes_0002... OK
  15. Applying auth.0007_alter_validators_add_error_messages... OK
  16. Applying auth.0008_alter_user_username_max_length... OK
  17. Applying sessions.0001_initial... OK
  18. C:\xiaohui\python-u8\demo\web\helloworld>python manage.py makemigrations polls
  19. Migrations for 'polls':
  20. polls\migrations\0001_initial.py
  21. - Create model Choice
  22. - Create model Question
  23. - Add field question to choice
  24. C:\xiaohui\python-u8\demo\web\helloworld>
  25. C:\xiaohui\python-u8\demo\web\helloworld>python manage.py sqlmigrate polls 0001
  26. BEGIN;
  27. --
  28. -- Create model Choice
  29. --
  30. CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL);
  31. --
  32. -- Create model Question
  33. --
  34. CREATE TABLE "polls_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text" varchar(200) NOT NULL, "pub_date" datetime NOT NULL);
  35. --
  36. -- Add field question to choice
  37. --
  38. ALTER TABLE "polls_choice" RENAME TO "polls_choice__old";
  39. CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL, "question_id" integer NOT NULL REFERENCES "polls_question" ("id"));
  40. INSERT INTO "polls_choice" ("choice_text", "votes", "id", "question_id") SELECT "choice_text", "votes", "id", NULL FROM "polls_choice__old";
  41. DROP TABLE "polls_choice__old";
  42. CREATE INDEX "polls_choice_question_id_c5b4b260" ON "polls_choice" ("question_id");
  43. COMMIT;
  44. C:\xiaohui\python-u8\demo\web\helloworld>
  45. C:\xiaohui\python-u8\demo\web\helloworld>python manage.py migrate
  46. Operations to perform:
  47. Apply all migrations: admin, auth, contenttypes, polls, sessions
  48. Running migrations:
  49. Applying polls.0001_initial... OK
  50. C:\xiaohui\python-u8\demo\web\helloworld>

python manage.py shell 交互命令,可以执行models 中相关方法 https://yiyibooks.cn/xx/Django_1.11.6/intro/tutorial02.html

管理后台

python manage.py createsuperuser

小结

Django 很强大,mvc,db 都已涉及,好好学习一下。