This is the 25th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Django is one of the best Python-based Web application development platforms available today, and this article takes a brief look at it and creates an empty Django project.

Before and after the end of the

  • The purpose of Web development can be said to provide users with a service scene that can obtain information or interact, which involves several important modules: display module, business processing module and data storage module.
Graph TB A(browser) subgraph server B[parse request] C[generate HTML page] D[business processing] end E(database) D-->C -- display --> A -- interaction --> B-->D-- data interaction --. -e
  • The big frame is that the user puts forward the service request to the server, the server receives the request and extracts the data from the database, sends it to the processing module to get the content that the user needs, and then shows the content in front of the user through the display module.
  • In this process, the part of data processing and business processing is called the back end, and the part of interactive logic that shows the content and design to users and the back end is called the front end.
  • In order to understand the coupling of business processing and interface display, after years of development experience of predecessors, the current best practice is to separate the front end and the back end, so that the front end personnel and the back end personnel agree on interface interaction, then the front end only needs to do the interface, the back end only needs to do business logic processing.

Django

Introduction to the

Django is an open source Web application framework written in Python. Its main purpose is to develop database-driven websites quickly and easily. There is a strong emphasis on code reuse. Multiple components can easily serve as “plug-ins” to the framework. Django has many powerful third-party plugins, and you can even easily develop your own toolkit.

  • Project source: github.com/django/djan…
  • Website: www.djangoproject.com/
  • Django Chinese website: www.django.cn/

version

The Django development team worked extremely hard, with tight release plans and frequent iterations

Release Series Latest Release End of mainstream support1 End of extended support2
4.2 LTS April 2023 December 2023 April 2026
4.1 August 2022 April 2023 December 2023
4.0 December 2021 August 2022 April 2023
3.2 LTS April 2021 December 2021 April 2024
3.1 3.1.5 April 2021 December 2021
3.0 3.0.11 August, 2020 April, 2021
2.2 LTS 2.2.17 December 2, 2019 April 2022
2.1 2.1.15 April 1, 2019 December 2, 2019
2.0 2.0.13 August 1, 2018 April 1, 2019
1.11 LTS 3 1.11.29 December 2, 2017 April 1, 2020
1.10 1.10.8 April 4, 2017 December 2, 2017
1.9 1.9.13 August 1, 2016 April 4, 2017
1.8 LTS 1.8.19 December 1, 2015 April 1, 2018
1.7 1.7.11 April 1, 2015 December 1, 2015
1.6 1.6.11 September 2, 2014 April 1, 2015
1.5 1.5.12 November 6, 2013 September 2, 2014
1.4 LTS 1.4.22 February 26, 2013 October 1, 2015
1.3 1.3.7 March 23, 2012 February 26, 2013

  • Django is developed in Python and runs in the Python environment. Different Django versions have different requirements for Python versions:
Django version Python version
1.8 2.7, 3.2 (until the end of 2016), 3.3, 3.4, 3.5
1.9, 1.10 2.7, 3.4, 3.5
1.11 2.7, 3.4, 3.5, 3.6, 3.7 (1.11.17 added)
2.0 3.4, 3.5, 3.6, 3.7
2.1, 2.2 3.5, 3.6, 3.7
3.0 3.6, 3.7, 3.8
3.1 3.6, 3.7, 3.8 or later

The installation

  • Determine which Django version you want. The current (January 23, 2021) Django version is 3.1.5, and my Python version is 3.8.3
  • Install with PIP:
pip install Django
Copy the code

PIP is installed with version 3.0.5 by default

  • If the latest version is required, you can specify:
PIP install Django = = 3.1.5Copy the code
  • Install website: www.djangoproject.com/download/
  • It can be used after installationpip showCommand to view package information:
pip show django

-->
Name: Django
Version: 3.0.5
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: d:\program_files\anaconda3\lib\site-packages
Requires: pytz, asgiref, sqlparse
Copy the code

or

python -m django --version

-->
3.0.5
Copy the code

Project creation

  • Find a folder where your Django project is stored, open a terminal there, and type the command:

Suppose we want to create a project named VVd_Django:

django-admin startproject vvd_django
Copy the code
  • The directory structure of the current project:
tree /F  # windows
tree -a  # Linux

-->└ ─ vvd_django│ the manage. Py └ ─ vvd_django asgi. Py Settings. Py urls. Py wsgi. Py just set pyCopy the code
  • These directories and files are useful for:
    • The outermost layer of thevvd_django/The root directory is just a container for your project. The root directory name doesn’t affect Django. You can rename it to anything you like.
    • manage.pyA command line tool that lets you manage Django projects in a variety of ways. You can readdjango-admin and manage.pyGet all themanage.pyThe details.
    • It’s on the insidevvd_django/The directory contains your project, which is a pure Python package. Its name is the name of the Python package you need when you reference anything inside it. (e.g.,vvd_django.urls).
    • vvd_django/__init__.py: an empty file that tells Python that the directory should be considered a Python package. If you are new to Python, read the official documentationLearn more about packages.
    • vvd_django/settings.py: Configuration file for a Django project. If you want to know how this file works, check it outDjango configurationGet the details.
    • vvd_django/urls.pyThe URL declaration for your Django project, like the “directory” for your web site. readingURL schedulerDocument to get more information about urls.
    • vvd_django/asgi.py: as an entry point for your project to run on an ASGi-compatible Web server. readingHow do I deploy with WSGIFor more details.
    • vvd_django/wsgi.py: as an entry point for your project to run on a WSGi-compatible Web server. readingHow do I deploy with WSGIFor more details.
    • Synchronizing database files:

Django’s default database is db.sqlite3

cd vvd_django
python manage.py migrate

-->
Operations to perform:
  Apply all migrations: admin, auth, 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 sessions.0001_initial... OK
Copy the code
  • Start Django Server and check the status of our current project:
python manage.py runserver

-->
Watching for file changes with StatReloaderPerforming system checks... System Check identified no issues (0 silenced). January 23, 2021-15:42:03 Django Version 3.0.5, Using Settings' VVd_django. Settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.Copy the code
  • At this point we use a browser to visit http://127.0.0.1:8000/

The small rocket shows that our first Django project is working properly.

Write the first view

The new application

  • Run in project root terminal:
python manage.py startapp hello_world
Copy the code
  • Current project file structure
│ db.sqlite3 │ manage.py │ ├─ Hello_world │ admin.py │ apps.py │ models.py │ tests.py │ views.py │ __init__ │ └ ─ migrations │ set py └ ─ vvd_django │ asgi. Py │ Settings. Py │ urls. Py │ wsgi. Py └ ─ just set pyCopy the code

The new view

Open hello_world/views.py and type in the following Python code:

from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse


def index(request) :
    return HttpResponse("Hello, world. You're at the vvd hello_world index.")
Copy the code

This is the simplest view in Django. To see the effect, we need to map a URL to it

Applying URL Mapping

  • inhello_worldCreate a new directoryurls.pyFile and enter the code:
from django.urls import path

from . import views

urlpatterns = [
    path(' ', views.index, name='index'),]Copy the code

The root url mapping

  • Specify what we created in the root URLconf filehello_world.urlsThe module. invvd_django/urls.pyOf the fileurlpatternsInsert one in the listinclude()That is as follows:
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('hello_world/', include('hello_world.urls')),
    path('admin/', admin.site.urls),
]

Copy the code

The function include() allows references to other URLconfs, and the idea is to make it plug-and-play. Whenever Django encounters include(), it truncates the portion of the URL that matches that item and sends the remaining string to URLconf for further processing. They can be placed in any path to make the application work.

Run the service

python manage.py runserver
Copy the code

To access the application

  • At this point we access our set upserever/hello_worldLink -http://localhost:8000/hello_world/

  • Indicates that our application was successfully invoked

The resources

  • Github.com/django/djan…
  • www.djangoproject.com/
  • www.django.cn/
  • Docs.djangoproject.com/zh-hans/3.0…
  • Blog.csdn.net/weixin_4166…