Django framework

                 Django                   

Installation (install the latest LTS version):

 

pip3 install django==1.11.9

 

Create a django project:

The following command creates a Django project named "mysite":

django-admin startproject mysite

Catalog introduction:

mysite/ 
├── manage.py #Management   files └── 
mysite   #Project directory 
    ├── __init__ .py
    ├── settings.py   # Configuration├── 
    urls.py #Routing   -- > Correspondence between URLs and functions 
    └── wsgi.py   # The runserver command uses the wsgiref module as a simple web server

Run the Django project:

python manage.py runserver 127.0.0.1:8000

Template file configuration:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "template")],  # template文件夹位置
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
copy code

Static file configuration:

STATIC_URL = ' /static/ '   # Static folder prefix used in HTML 
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, " static " ),   #Static file storage location 
]

Do not understand? There are pictures and facts:

 

                  Django's basic three-piece set:                    

from django.shortcuts import HttpResponse, render, redirect

 

HttpResponse

Internally, a string parameter is passed in and returned to the browser.

E.g:

def index(request):
     #Business logic code 
    return HttpResponse( " OK " )

redirect

Accepts a URL parameter, which means to jump to the specified URL.

E.g:

def index(request):
    # business logic code
    return redirect("/home/")

What's up with the redirect?

 

Start Django and report an error:

Django starts with an error UnicodeEncodeError...

This error is usually reported because the computer name is Chinese. Change the computer name to English and restart the computer.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324981525&siteId=291194637