python web development framework

A brief
HTTP: stateless, short connections (links automatically disconnected after)
TCP: After setting close links unless otherwise constantly open
web (application):
Browser: (socket client)
visited URLs: (socket Service end)

程序流程:
1、 客户端输入网址(ip:port), 访问网址
	链接服务端并发送请求
2、 服务端监听自己的ip和port,监听到之后,就可以接收到客户端发来的请求, 并给于响应, 响应之后断开链接
3、客户端接收到服务端的相应之后,也断开链接

Write your own website:
1, socket server
2, based on unreasonable url returns different pages
routing system:
url -> Function
3, the user returns the string
template engine to render
html serve as a template (set of special characters in a specific location, dynamic database acquiring data, the program generates the tag information specifying the location of replaced Html special characters)
. 4, Web frame:
contains (1, 2, 3) functions of respective parts: Tornado
contain features Example (2, 3) part: Django (wsgiref for third-party tools to achieve functional 1)
contains the function (2) part: Flask
these frameworks is the only heavyweight Django framework, others are lightweight framework

Two, Django framework:
1, install django, django PIP3 install / django Conda install
2, create a project django:
django-ADMIN startproject mysite (project folder name)
3, to start the project created:
Python manage.py the runserver 127.0.0.1: 8080
the default port is: 8000
4, PyCharm create project Contents introduction

	mysite
		mysite
			settings.py:  #  django的配置文件
			url.py:  # 路由系统,url和函数的映射关系
			wsgi.py: # socket功能,本机带有wsgiref,工程项目生产中使用 uwsgi
			
	# 当前项目中所有的操作都有该程序管理
	manage.py:   所有程序的管理程序脚本

Third, the simple login page:
1, url.py: Create a function mapping function, you must pass in a parameter, parameter information for all requests
Here Insert Picture Description
2, templates: create a page template file in the file,
Here Insert Picture Description
and setting the configuration information:
Here Insert Picture Descriptionthe main DIRS specified value, is the file html file folder location

3、创建static文件夹, 编写静态文件, css:  设定页面模板文件的样式,在html文件中使用link将css中的样式加入页面展示中

Here Insert Picture Description
4, additional configuration:
HTML page template that you want to introduce css styles, you need to configure the following information in setting.py in:

# 使用是的前缀
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

static file folder location of css file, no need to locate a specific css files, but html css styles introduced when the position location using information STATIC_URL: The
recommended storage time css create folders, folder names and setting .py the same value in STATIC_URL

<link rel="stylesheet" href="/static/commons.css">
	还有一个额外配置:
将第五行注释掉
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    #'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Published 12 original articles · won praise 1 · views 1581

Guess you like

Origin blog.csdn.net/qq_43031234/article/details/104232003