Refer to the official Django documentation (2.2), based on Python3.5+Django2.2

The installation

$PIP install django==2.2 $python -m django --versionCopy the code

Create a project

Mysite/manage.py mysite/ __init__.py settings.py urls.py wsgi.pyCopy the code

These directories and files are useful for:

  • The outer mysite/ root directory is just a container for your project. Django doesn’t care what its name is. You can rename it to anything you like.
  • Manage.py: a command line tool that lets you manage Django projects in a variety of ways. You can read Django-admin and manage.py for all the details of manage.py.
  • The mysite/ directory in the inner layer 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. (such as mysite. Urls).
  • Mysite /__init__.py: an empty file that tells Python that the directory should be considered a Python package. If you’re new to Python, read more about packages in the official documentation.
  • Mysite /settings.py: configuration file for your Django project. If you want to know how this file works, check out the Django configuration for details.
  • Mysite /urls.py: The URL declaration for your Django project, like the “directory” for your site. Read the URL scheduler documentation for more information about urls.
  • Mysite /wsgi.py: Act as an entry point for your project to run on a WSGi-compatible Web server. Read how to deploy using WSGI for more details.

Start the project

$python manage.py runServer [port] # Default port 8000 can be specified manuallyCopy the code

This RunServer is just a simple Web service, intended for debugging only, not for use in a real production environment. (Real production environments use services such as Apache)

Create an

In Django, every application is a Python package and follows the same conventions. Django comes with a tool that will help you generate your application’s basic directory structure so you can focus on writing code instead of creating directories.

What is the difference between a project and an application? An application is a web application that does just one thing — like a blogging system, or a database of public records, or a simple voting program. A project is a collection of configurations and applications used by a site. Projects can contain many applications. Applications can be used by many projects.

$python manage.py startapp polls # Directory structure polls/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.pyCopy the code

Writing views

View can be understood as the method of lock execution after url matching route. The specific writing method is as follows

In polls/views.py, write the following code

from django.http import HttpResponse


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

To see the results, we need to map a URL to it and create a new urls.py file in the Polls directory

In polls/urls.py, type the following code

from django.urls import path

from . import views

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

When you’re done, you need to insert the applied URLconfs into the project’s URLconfs

In mysite/urls. Py

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]
Copy the code

Validate your service

Once the service is up and running, it accesses your interface in the browser