1. Prepare the environment
> python -V Python 3.7.3 > pip -V pip 20.0.2 from c:\python3\lib\site-packages\pip (python 3.7) > pip install django > python -m django --version 2.2.9
2. Create an along project
> django-admin startproject along
An along directory will be created. Its directory structure is as follows:
along/ manage.py along/ __init__.py settings.py urls.py asgi.py wsgi.py
- The external along / root directory is the container of the project, and its name is irrelevant to Django. You can rename it to any name you like;
- manage.py:A command line tool that allows you to manage Django projects in various ways. You can read Django admin and manage.py Get all manage.py Details. You can also use python\ manage.py -H for usage.
- The along / directory on the inner layer contains your project, which is a pure Python package. Its name is the python package name you need to use when you refer to anything inside it. (for example along.urls .
- along/__ init__ . py: an empty file that tells python that this directory should be considered a python package.
- along/ settings.py : the configuration file for the Django project. If you want to know how this file works, check out Django configuration for details.
- along/urls.py：Django project URL statement, just like your websiteTable of contents。 Read the URL scheduler documentation to learn more about URLs.
- along/ asgi.py : an entry point for an asgi compliant web server to provide services for your project.
- along/ wsgi.py : as the entry point for your project to run on a WSGI compliant web server.
3. Create Hello world application
> cd .\along\
> python manage.py startapp hello
A hello directory will be created. Its directory structure is as follows:
hello/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py
4. Write Hello views
Write along/ views.py
from django.shortcuts import render from django.http import HttpResponse def index(request): return HttpResponse("Hello world~")
This is the simplest view in Django. If we want to see the effect, we need to put aURL mappingTo it – that’s why we need urlconf.
5. urls mapping
(1) Create a new one in the Hello directory urls.py Documents
from django.urls import path from . import views urlpatterns = [ path('', views.index), ]
(2) The next step is to specify what we created in the root urlconf file hello.urls Module. In along/ urls.py Insert an “include() in the” urlpatterns “list of the file, as follows:
from django.contrib import admin from django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), path('hello/', include('hello.urls')), ]
- The function ‘include() allows references to other urlconfs. Whenever Django encounters’ include(), it truncates the part of the URL that matches the entry and sends the remaining string to urlconf for further processing.
- The idea of include () is to make it plug and play. Because the Hello application has its own urlconf (Hello/ urls.py ), they can be placed in any other path, and the application will work properly
(3) Path() parameter
The function path() has four parameters, two required parameters:Route and view, two optional parameters:Kwargs and name。
- Path() parameter: route
routeIt’s aMatch URLThe rule of (similar to regular expressions). When Django responds to a request, it starts with the first item of “urlpatterns.”,Match in orderList until a matching item is found.
These criteria do not match get and post parameters or domain names; for example:
Urlconf is processing the request https://www.example.com/myapp/ It tries to match myapp/
Processing requests https://www.example.com/myapp/?page=3 Only attempts to match myapp /.
- Path() parameter: View
When Django finds a matching criterion, it willCall this particular view functionAn httprequest object is passed in as the first parameter. The captured parameter is passed in as a keyword parameter
- Path() parameter: kwargs
AnyKeyword parameterCan be passed as a dictionary to the target view function
- Path() parameter: name
For youURL namingIt allows you to uniquely reference it anywhere in Django, especially in templates. This useful feature allows you to change a URL pattern globally by changing only one file.
6. Run call excuse test
(1) Start service
> python manage.py runserver 127.0.0.1:9000
- 127.0.0.1: listening address
- 9000: listening port
(2) Web access test
(3) Command line access
> curl 127.0.0.1:9000/hello
(4) Python call excuse
import requests url = "http://127.0.0.1:9000/hello/" result = requests.get(url) print(result.content)