跟老齐学Python Django实战 3/n Second edition

#start project

1.1, venv module is embedied since python3.3, the right way to start is:

"

python3 -m venv /path/to/new/virtual/environment

"

cor@debian:~/laoqidjango$ python3.5 -m venv lqdjango0601
cor@debian:~/laoqidjango$ ls
lqdjango0601
cor@debian:~/laoqidjango$ tree lqdjango0601/
lqdjango0601/
├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── easy_install
│   ├── easy_install-3.5
│   ├── pip
│   ├── pip3
│   ├── pip3.5
│   ├── python -> python3.5
│   ├── python3 -> python3.5
│   └── python3.5 -> /usr/bin/python3.5
├── include
├── lib
│   └── python3.5
│       └── site-packages
│           ├── easy_install.py
│           ├── pip
│           │   ├── basecommand.py
│           │   ├── baseparser.py
│           │   ├── cmdoptions.py
│           │   ├── commands
│           │   │   ├── check.py
│           │   │   ├── completion.py
│           │   │   ├── download.py
│           │   │   ├── freeze.py
│           │   │   ├── hash.py
│           │   │   ├── help.py
│           │   │   ├── __init__.py
│           │   │   ├── install.py
│           │   │   ├── list.py
│           │   │   ├── __pycache__
│           │   │   │   ├── check.cpython-35.pyc
│           │   │   │   ├── completion.cpython-35.pyc
│           │   │   │   ├── download.cpython-35.pyc
│           │   │   │   ├── freeze.cpython-35.pyc
│           │   │   │   ├── hash.cpython-35.pyc
│           │   │   │   ├── help.cpython-35.pyc
│           │   │   │   ├── __init__.cpython-35.pyc
│           │   │   │   ├── install.cpython-35.pyc
│           │   │   │   ├── list.cpython-35.pyc
│           │   │   │   ├── search.cpython-35.pyc
│           │   │   │   ├── show.cpython-35.pyc
│           │   │   │   ├── uninstall.cpython-35.pyc
│           │   │   │   └── wheel.cpython-35.pyc
│           │   │   ├── search.py
│           │   │   ├── show.py
│           │   │   ├── uninstall.py
│           │   │   └── wheel.py
│           │   ├── compat
│           │   │   ├── dictconfig.py
│           │   │   ├── __init__.py
│           │   │   └── __pycache__
│           │   │       ├── dictconfig.cpython-35.pyc
│           │   │       └── __init__.cpython-35.pyc
│           │   ├── download.py
│           │   ├── exceptions.py
│           │   ├── index.py
│           │   ├── __init__.py
│           │   ├── locations.py
│           │   ├── __main__.py
│           │   ├── models
│           │   │   ├── index.py
│           │   │   ├── __init__.py
│           │   │   └── __pycache__
│           │   │       ├── index.cpython-35.pyc
│           │   │       └── __init__.cpython-35.pyc
│           │   ├── operations
│           │   │   ├── check.py
│           │   │   ├── freeze.py
│           │   │   ├── __init__.py
│           │   │   └── __pycache__
│           │   │       ├── check.cpython-35.pyc
│           │   │       ├── freeze.cpython-35.pyc
│           │   │       └── __init__.cpython-35.pyc
│           │   ├── pep425tags.py
│           │   ├── __pycache__
│           │   │   ├── basecommand.cpython-35.pyc
│           │   │   ├── baseparser.cpython-35.pyc
│           │   │   ├── cmdoptions.cpython-35.pyc
│           │   │   ├── download.cpython-35.pyc
│           │   │   ├── exceptions.cpython-35.pyc
│           │   │   ├── index.cpython-35.pyc
│           │   │   ├── __init__.cpython-35.pyc
│           │   │   ├── locations.cpython-35.pyc
│           │   │   ├── __main__.cpython-35.pyc
│           │   │   ├── pep425tags.cpython-35.pyc
│           │   │   ├── status_codes.cpython-35.pyc
│           │   │   └── wheel.cpython-35.pyc
│           │   ├── req
│           │   │   ├── __init__.py
│           │   │   ├── __pycache__
│           │   │   │   ├── __init__.cpython-35.pyc
│           │   │   │   ├── req_file.cpython-35.pyc
│           │   │   │   ├── req_install.cpython-35.pyc
│           │   │   │   ├── req_set.cpython-35.pyc
│           │   │   │   └── req_uninstall.cpython-35.pyc
│           │   │   ├── req_file.py
│           │   │   ├── req_install.py
│           │   │   ├── req_set.py
│           │   │   └── req_uninstall.py
│           │   ├── status_codes.py
│           │   ├── utils
│           │   │   ├── appdirs.py
│           │   │   ├── build.py
│           │   │   ├── deprecation.py
│           │   │   ├── encoding.py
│           │   │   ├── filesystem.py
│           │   │   ├── glibc.py
│           │   │   ├── hashes.py
│           │   │   ├── __init__.py
│           │   │   ├── logging.py
│           │   │   ├── outdated.py
│           │   │   ├── packaging.py
│           │   │   ├── __pycache__
│           │   │   │   ├── appdirs.cpython-35.pyc
│           │   │   │   ├── build.cpython-35.pyc
│           │   │   │   ├── deprecation.cpython-35.pyc
│           │   │   │   ├── encoding.cpython-35.pyc
│           │   │   │   ├── filesystem.cpython-35.pyc
│           │   │   │   ├── glibc.cpython-35.pyc
│           │   │   │   ├── hashes.cpython-35.pyc
│           │   │   │   ├── __init__.cpython-35.pyc
│           │   │   │   ├── logging.cpython-35.pyc
│           │   │   │   ├── outdated.cpython-35.pyc
│           │   │   │   ├── packaging.cpython-35.pyc
│           │   │   │   ├── setuptools_build.cpython-35.pyc
│           │   │   │   └── ui.cpython-35.pyc
│           │   │   ├── setuptools_build.py
│           │   │   └── ui.py
│           │   ├── vcs
│           │   │   ├── bazaar.py
│           │   │   ├── git.py
│           │   │   ├── __init__.py
│           │   │   ├── mercurial.py
│           │   │   ├── __pycache__
│           │   │   │   ├── bazaar.cpython-35.pyc
│           │   │   │   ├── git.cpython-35.pyc
│           │   │   │   ├── __init__.cpython-35.pyc
│           │   │   │   ├── mercurial.cpython-35.pyc
│           │   │   │   └── subversion.cpython-35.pyc
│           │   │   └── subversion.py
│           │   ├── _vendor
│           │   │   ├── __init__.py
│           │   │   └── __pycache__
│           │   │       └── __init__.cpython-35.pyc
│           │   └── wheel.py
│           ├── pip-9.0.1.dist-info
│           │   ├── DESCRIPTION.rst
│           │   ├── entry_points.txt
│           │   ├── INSTALLER
│           │   ├── METADATA
│           │   ├── metadata.json
│           │   ├── RECORD
│           │   ├── top_level.txt
│           │   └── WHEEL
│           ├── pkg_resources
│           │   ├── extern
│           │   │   ├── __init__.py
│           │   │   └── __pycache__
│           │   │       └── __init__.cpython-35.pyc
│           │   ├── __init__.py
│           │   ├── __pycache__
│           │   │   └── __init__.cpython-35.pyc
│           │   └── _vendor
│           │       ├── appdirs.py
│           │       ├── __init__.py
│           │       ├── packaging
│           │       │   ├── __about__.py
│           │       │   ├── _compat.py
│           │       │   ├── __init__.py
│           │       │   ├── markers.py
│           │       │   ├── __pycache__
│           │       │   │   ├── __about__.cpython-35.pyc
│           │       │   │   ├── _compat.cpython-35.pyc
│           │       │   │   ├── __init__.cpython-35.pyc
│           │       │   │   ├── markers.cpython-35.pyc
│           │       │   │   ├── requirements.cpython-35.pyc
│           │       │   │   ├── specifiers.cpython-35.pyc
│           │       │   │   ├── _structures.cpython-35.pyc
│           │       │   │   ├── utils.cpython-35.pyc
│           │       │   │   └── version.cpython-35.pyc
│           │       │   ├── requirements.py
│           │       │   ├── specifiers.py
│           │       │   ├── _structures.py
│           │       │   ├── utils.py
│           │       │   └── version.py
│           │       ├── __pycache__
│           │       │   ├── appdirs.cpython-35.pyc
│           │       │   ├── __init__.cpython-35.pyc
│           │       │   ├── pyparsing.cpython-35.pyc
│           │       │   └── six.cpython-35.pyc
│           │       ├── pyparsing.py
│           │       └── six.py
│           ├── pkg_resources-0.0.0.dist-info
│           │   ├── DESCRIPTION.rst
│           │   ├── INSTALLER
│           │   ├── METADATA
│           │   ├── metadata.json
│           │   ├── RECORD
│           │   └── WHEEL
│           ├── __pycache__
│           │   └── easy_install.cpython-35.pyc
│           ├── setuptools
│           │   ├── archive_util.py
│           │   ├── cli-32.exe
│           │   ├── cli-64.exe
│           │   ├── cli.exe
│           │   ├── command
│           │   │   ├── alias.py
│           │   │   ├── bdist_egg.py
│           │   │   ├── bdist_rpm.py
│           │   │   ├── bdist_wininst.py
│           │   │   ├── build_ext.py
│           │   │   ├── build_py.py
│           │   │   ├── develop.py
│           │   │   ├── easy_install.py
│           │   │   ├── egg_info.py
│           │   │   ├── __init__.py
│           │   │   ├── install_egg_info.py
│           │   │   ├── install_lib.py
│           │   │   ├── install.py
│           │   │   ├── install_scripts.py
│           │   │   ├── launcher manifest.xml
│           │   │   ├── py36compat.py
│           │   │   ├── __pycache__
│           │   │   │   ├── alias.cpython-35.pyc
│           │   │   │   ├── bdist_egg.cpython-35.pyc
│           │   │   │   ├── bdist_rpm.cpython-35.pyc
│           │   │   │   ├── bdist_wininst.cpython-35.pyc
│           │   │   │   ├── build_ext.cpython-35.pyc
│           │   │   │   ├── build_py.cpython-35.pyc
│           │   │   │   ├── develop.cpython-35.pyc
│           │   │   │   ├── easy_install.cpython-35.pyc
│           │   │   │   ├── egg_info.cpython-35.pyc
│           │   │   │   ├── __init__.cpython-35.pyc
│           │   │   │   ├── install.cpython-35.pyc
│           │   │   │   ├── install_egg_info.cpython-35.pyc
│           │   │   │   ├── install_lib.cpython-35.pyc
│           │   │   │   ├── install_scripts.cpython-35.pyc
│           │   │   │   ├── py36compat.cpython-35.pyc
│           │   │   │   ├── register.cpython-35.pyc
│           │   │   │   ├── rotate.cpython-35.pyc
│           │   │   │   ├── saveopts.cpython-35.pyc
│           │   │   │   ├── sdist.cpython-35.pyc
│           │   │   │   ├── setopt.cpython-35.pyc
│           │   │   │   ├── test.cpython-35.pyc
│           │   │   │   ├── upload.cpython-35.pyc
│           │   │   │   └── upload_docs.cpython-35.pyc
│           │   │   ├── register.py
│           │   │   ├── rotate.py
│           │   │   ├── saveopts.py
│           │   │   ├── sdist.py
│           │   │   ├── setopt.py
│           │   │   ├── test.py
│           │   │   ├── upload_docs.py
│           │   │   └── upload.py
│           │   ├── config.py
│           │   ├── depends.py
│           │   ├── dist.py
│           │   ├── extension.py
│           │   ├── extern
│           │   │   ├── __init__.py
│           │   │   └── __pycache__
│           │   │       └── __init__.cpython-35.pyc
│           │   ├── glob.py
│           │   ├── gui-32.exe
│           │   ├── gui-64.exe
│           │   ├── gui.exe
│           │   ├── __init__.py
│           │   ├── launch.py
│           │   ├── lib2to3_ex.py
│           │   ├── monkey.py
│           │   ├── msvc.py
│           │   ├── namespaces.py
│           │   ├── package_index.py
│           │   ├── py26compat.py
│           │   ├── py27compat.py
│           │   ├── py31compat.py
│           │   ├── py33compat.py
│           │   ├── py36compat.py
│           │   ├── __pycache__
│           │   │   ├── archive_util.cpython-35.pyc
│           │   │   ├── config.cpython-35.pyc
│           │   │   ├── depends.cpython-35.pyc
│           │   │   ├── dist.cpython-35.pyc
│           │   │   ├── extension.cpython-35.pyc
│           │   │   ├── glob.cpython-35.pyc
│           │   │   ├── __init__.cpython-35.pyc
│           │   │   ├── launch.cpython-35.pyc
│           │   │   ├── lib2to3_ex.cpython-35.pyc
│           │   │   ├── monkey.cpython-35.pyc
│           │   │   ├── msvc.cpython-35.pyc
│           │   │   ├── namespaces.cpython-35.pyc
│           │   │   ├── package_index.cpython-35.pyc
│           │   │   ├── py26compat.cpython-35.pyc
│           │   │   ├── py27compat.cpython-35.pyc
│           │   │   ├── py31compat.cpython-35.pyc
│           │   │   ├── py33compat.cpython-35.pyc
│           │   │   ├── py36compat.cpython-35.pyc
│           │   │   ├── sandbox.cpython-35.pyc
│           │   │   ├── site-patch.cpython-35.pyc
│           │   │   ├── ssl_support.cpython-35.pyc
│           │   │   ├── unicode_utils.cpython-35.pyc
│           │   │   ├── version.cpython-35.pyc
│           │   │   └── windows_support.cpython-35.pyc
│           │   ├── sandbox.py
│           │   ├── script (dev).tmpl
│           │   ├── script.tmpl
│           │   ├── site-patch.py
│           │   ├── ssl_support.py
│           │   ├── unicode_utils.py
│           │   ├── version.py
│           │   └── windows_support.py
│           └── setuptools-33.1.1.dist-info
│               ├── dependency_links.txt
│               ├── DESCRIPTION.rst
│               ├── entry_points.txt
│               ├── INSTALLER
│               ├── METADATA
│               ├── metadata.json
│               ├── RECORD
│               ├── top_level.txt
│               ├── WHEEL
│               └── zip-safe
├── lib64 -> lib
├── pyvenv.cfg
└── share
    └── python-wheels
        ├── appdirs-1.4.0-py2.py3-none-any.whl
        ├── CacheControl-0.11.7-py2.py3-none-any.whl
        ├── chardet-2.3.0-py2.py3-none-any.whl
        ├── colorama-0.3.7-py2.py3-none-any.whl
        ├── distlib-0.2.4-py2.py3-none-any.whl
        ├── distro-1.0.1-py2.py3-none-any.whl
        ├── html5lib-0.999999999-py2.py3-none-any.whl
        ├── ipaddress-0.0.0-py2.py3-none-any.whl
        ├── lockfile-0.12.2-py2.py3-none-any.whl
        ├── packaging-16.8-py2.py3-none-any.whl
        ├── pip-9.0.1-py2.py3-none-any.whl
        ├── pkg_resources-0.0.0-py2.py3-none-any.whl
        ├── progress-1.2-py2.py3-none-any.whl
        ├── pyparsing-2.1.10-py2.py3-none-any.whl
        ├── requests-2.12.4-py2.py3-none-any.whl
        ├── retrying-1.3.3-py2.py3-none-any.whl
        ├── setuptools-33.1.1-py2.py3-none-any.whl
        ├── six-1.10.0-py2.py3-none-any.whl
        ├── urllib3-1.19.1-py2.py3-none-any.whl
        ├── webencodings-0.5-py2.py3-none-any.whl
        └── wheel-0.29.0-py2.py3-none-any.whl

44 directories, 306 files

  1.2. start 

cor@debian:~/laoqidjango/lqdjango0601$ source bin/activate
(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601$

  

       1.3. "django-admin startproject mysite"

(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601$ django-admin startproject mysite
(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601$ ls
bin  include  lib  lib64  mysite  pyvenv.cfg  share
(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601$ ls mysite/
manage.py  mysite
(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601$ tree mysite/
mysite/
├── manage.py
└── mysite
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

1 directory, 5 files

  1.4. "python3.5 manage.py runserver"

(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601$ cd mysite/
(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601/mysite$ ls
manage.py  mysite
(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601/mysite$ python3.5 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.

June 01, 2020 - 04:03:19
Django version 2.2.12, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Error: That port is already in use.

  1.5. "python3.5 manage.py runserver"&&"python3.5 manage.py runserver addrport:7000"

(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601/mysite$ python3.5 manage.py runserver addrport:7000
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.

June 01, 2020 - 04:14:14
Django version 2.2.12, using settings 'mysite.settings'
Starting development server at http://addrport:7000/
Quit the server with CONTROL-C.
Error: [Errno -2] Name or service not known

  1.6. "python3.5 manage.py runserver 7000"  

# not using the default port 8000

(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601/mysite$ python3.5 manage.py runserver 7000
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.

June 01, 2020 - 04:46:43
Django version 2.2.12, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:7000/
Quit the server with CONTROL-C.

# successfully finally

2 start application

#before

(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601/mysite$ ls
db.sqlite3  manage.py  mysite
(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601/mysite$ tree
.
├── db.sqlite3
├── manage.py
└── mysite
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-35.pyc
    │   ├── settings.cpython-35.pyc
    │   ├── urls.cpython-35.pyc
    │   └── wsgi.cpython-35.pyc
    ├── settings.py
    ├── urls.py
    └── wsgi.py

2 directories, 10 files

  2.1 "python3.5 manage.py startapp blog"

#after

(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601/mysite$ python3.5 manage.py startapp blog
(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601/mysite$ tree
.
├── blog
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── manage.py
└── mysite
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-35.pyc
    │   ├── settings.cpython-35.pyc
    │   ├── urls.cpython-35.pyc
    │   └── wsgi.cpython-35.pyc
    ├── settings.py
    ├── urls.py
    └── wsgi.py

4 directories, 17 files

  "python3.5 manage.py makemigrations"

(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601/mysite$  python3.5 manage.py makemigrations
Migrations for 'blog':
  blog/migrations/0001_initial.py
    - Create model BlogArticles
(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601/mysite$ ls blog/migrations/
0001_initial.py  __init__.py  __pycache__

  "python3.5 manage.py migrate"

(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601/mysite$ python3.5 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying blog.0001_initial... OK
  Applying sessions.0001_initial... OK

  "pragma table_info(blog_blogarticles);"

(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601/mysite$ sqlite3  ./db.sqlite3 
SQLite version 3.16.2 2017-01-06 16:32:41
Enter ".help" for usage hints.
sqlite> .tables
auth_group                  blog_blogarticles         
auth_group_permissions      django_admin_log          
auth_permission             django_content_type       
auth_user                   django_migrations         
auth_user_groups            django_session            
auth_user_user_permissions
sqlite> pragma table_info(blog_blogarticles);
0|id|integer|1||1
1|title|varchar(300)|1||0
2|body|text|1||0
3|publish|datetime|1||0
4|author_id|integer|1||0
sqlite>

  

  "python3.5 manage.py createsuperuser"

(lqdjango0601) cor@debian:~/laoqidjango/lqdjango0601/mysite$ python3.5 manage.py createsuperuser
Username (leave blank to use 'cor'): cor
Email address: 123@126.com
Password: 
Password (again): 
This password is too short. It must contain at least 8 characters.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.

  

原文地址:https://www.cnblogs.com/winditsway/p/13029728.html