Initial Django – Hello World


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. Create an along project

> django-admin startproject along

An along directory will be created. Its directory structure is as follows:



  • 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;
  • command line tool that allows you to manage Django projects in various ways. You can read Django admin and Get all Details. You can also use python\ -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/ : the configuration file for the Django project. If you want to know how this file works, check out Django configuration for details.
  • along/urls.pyDjango project URL statement, just like your websiteTable of contents。 Read the URL scheduler documentation to learn more about URLs.
  • along/ : an entry point for an asgi compliant web server to provide services for your project.
  • along/ : as the entry point for your project to run on a WSGI compliant web server.

3. Create Hello world application

> cd .\along\

> python startapp hello

A hello directory will be created. Its directory structure is as follows:



4. Write Hello views

Write along/

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 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/ 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('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/ ), 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 It tries to match myapp/

Processing requests 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 runserver


  • listening address
  • 9000: listening port


(2) Web access test


(3) Command line access

> curl


(4) Python call excuse

import requests

url = ""
result = requests.get(url)