[Django] installation and configuration --2019-08-17 05:57:35

Original: http://blog.gqylpy.com/gqy/259

"@
Django official website to download
***

MVC framework with MTV frame

MVC, full name Model View Controller, software engineering is a software architecture model.
The software system is divided into three basic parts: == model (Model), view (View), Controller (Controller) ==

advantage : low coupling, high reusability, and low life-cycle cost.
Here Insert Picture Description
Django framework adopts the idea of the design pattern MVC architecture is divided into three parts, to reduce the coupling between the various parts.
differs in that it Django framework is split into three parts: == model (model), template (template), view (view) ==, is the MTV == == frame.

***

Django's MTV mode

  • Model (model): Responsible for business objects and database objects (ORM).
  • Template (template): responsible for how the page to the user.
  • View (View): is responsible for the business logic, and calls the Model and Template at the appropriate time.

In addition, Django there is a dispatcher == == urls, it is the role of a URL of a page view requests distributed to different treatment, and then call the appropriate view of Model and Template.
***

Django framework shown

Here Insert Picture Description


***

Installation and Configuration

在命令行执行如下命令:
pip3 install django==1.11.11

创建一个Django项目

执行如下命令创建一个名为"mysite"的Django项目:
django-admin startproject mysite
***

目录介绍

Here Insert Picture Description
***

运行Django项目

命令行执行:
python manage.py runserver 127.0.0.1:8080
***

启动Django报错

==UnicodeEncodeError...==
出现这种错误通常是因为计算机名为中文,将其改为中文并重启电脑便可以了.

==SyntaxError: Generator expression must be parenthesized==
保这个错误很大可能是因为使用了Python3.7.0.
目前(2018-06-12)Python3.7.0和Django还有点兼容性问题,换回Python3.6环境即可.
***

模版文件配置

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]   # 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',
            ],
        },
    },
]

静态文件配置

STATIC_URL = '/static/'  # HTML中使用的静态文件夹前缀
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),  # 静态文件存放位置
]

关系如图:
Here Insert Picture Description

刚开始学习时可在配置文件中注释掉csrf中间件,方便表单提交测试:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',  # csrf中间件
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]


APP的创建

一个Django项目可以分为很多个APP,用来隔离不同功能模块的代码.

方式一:命令行创建:

python manage.py startapp app名称

注意当前所在路径,必须在项目的根目录下.

方式二:使用PyCharm创建:
Here Insert Picture Description
点击:Tools --> Run manage.py Task...
然后在下方弹出的命令窗口中输入:==startapp app名称==

Is created after, also you need to add the corresponding APP in settings.py file.
As follows:

# 本项目中的所有APP
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog.apps.BlogConfig',   # 添加自己新建的app(可简写为'blog')
]

Creating a superuser

Note: You must after migrating data before they can successfully create a super user.

python manage.py createsuperuser

After entering the above command, follow the prompts to enter a user name, email, password, confirm the password. The password is not required at least eight, and mailboxes can not be too close to twice the password needs to be consistent.

Djange essential foundation Three Musketeers

from django.shortcuts import HttpResponse, render, redirect

HttpResponse

== == returns a specified string
inside pass a string parameter, it will help us to do the right job (such as: return a response status line), then the string passed back to the browser.

as follows:

def index(request):
    # 业务逻辑代码
    return HttpResponse('OK')

render

== == returns an HTML document
in addition to receiving the request parameter, but also receives a template file to be rendered and a specific data dictionary stored parameters.
The data is filled into the template file, the final results returned to the browser.

as follows:

def index(request):
    # 业务逻辑代码
    return render(request, 'index.html', {'name': 'zyk', 'sex': 'boy'})

redirect

Jump to a specific page == ==
i.e. redirect, a URL parameter indicates the jump to a specific URL ..

as follows:

def index(request):
    # 业务逻辑代码
    return redirect('/home/')

What Redirection?

Here Insert Picture Description
Here Insert Picture Description


"

Original: http://blog.gqylpy.com/gqy/259

Guess you like

Origin www.cnblogs.com/bbb001/p/11367264.html