Introduction to Django
There are many different models of Web framework Python. Django is a heavyweight in the most representative one. Many successful website and APP are based on Django. Django Web application framework is an open source, written in Python.
Django software using MVC design pattern, i.e. the model M, and a controller C. view V
Django installation
Recommended download version 1.11 or so, LTS version support updates and provide technical assistance, it is recommended to use with LTS version.
Method 1: at the command line: pip3 install django == 1.11
Or install Django == 1.11 -i PIP http://pypi.hustunique.org/simple specify the version number, designated domestic mirror
Second way: with mounting pycharm
Three ways: with a Terminal command-line installation of pycharm
Check whether the installation was successful
Enter the following command to check:
python3
import django
django.get_version()
输出:
'1.11.11'
When the output of the version number of the installation is successful.
Create a django project
Mode 1 (command line to create):
Create a django project
django-admin startproject 项目名
Creating app application
在命令行中切到项目路径中执行
python3 manage.py startapp app01
Start django project
在命令行中切到项目路径中执行
python3 manage.py runserver
ps: django create the command line does not automatically create default templates folder
you need to manually create yourself (note change whether the folder path is added in the configuration file)
Way 2 (pycharm created):
Create a django project
FILE >>> new project 选择第二个django 需要注意名字不能有中文,选择本地的解释器,勾选后台管理
Creating app application
方式1:pycharm命令行创建
python3 manage.py startapp app01
方式2:Tools下面run manage task功能栏创建
starapp app02
Start django project
点击按钮启动
He stressed:
1. django must ensure that only a running state
2. Be sure to remember your browser's cache clear
Test project start
Access: When http://127.0.0.1:8000/ can see: IT worked! Item start successfully.
Port here to fill in their own settings.
django role of each file :
--应用名:
--migrations:数据库迁移记录相关数据
--admin.py:jango后台管理相关
--models.py :模型表相关
--views.py:视图函数相关
--项目名:
--__ init __.py:一个空文件,告诉 Python 该目录是一个 Python 包。
--settings.py: 该 Django 项目的设置/配置。
--urls.py:路由与视图函数的映射关系,该 Django 项目的 URL 声明; 一份由 Django 驱动的网站"目录"。
--wsgi.py: 一个 WSGI 兼容的 Web 服务器的入口,以便运行你的项目。
--templates:
项目用到的所有的html文件
--manage.py
django入口文件,一个实用的命令行工具,可让你以各种方式与该 Django 项目进行交互。
note
Note that the newly created app requires registration to take effect in the configuration file
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01.apps.App01Config' # 可以用全称
'app01' # 也可以简写
]
After creating a project to confirm whether settings.py configuration file TEMPLATES
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],# 配置自己的html文件路径
'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',
],
},
},
]
web request Flowchart:
Django will be three tricks
views.py in applications created to create a view
from django.shortcuts import render,HttpResponse,redirect
HttpResponse: Returns a string
def index(request):
return HttpResponse("hello LinWoW!")
render: Returns a html page, two ways to pass the front page value
def index(request):
user_dict = {'name':'linwow','password':'123'}
return render(request,'index.html',{'user_dict':user_dict})
def index(request):
user_dict = {'name':'linwow','password':'123'}
return render(request,'index.html',locals())
redirect: Redirect
def login(request):
return redirect('https://blog.csdn.net/linwow')
Django small example
Rendering read data from the database to html
first step:
Manually create project fristdjango, create app01, create templates folder and create index.html, the code can refer to: https://blog.csdn.net/linwow/article/details/90921053
Step two:
Adding templates path settings, the newly created app need to be registered in the configuration file.
third step:
Creating index view views.py under app01 file
def index(request):
conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='mysql',
database='jinja',
charset='utf8',
autocommit=True,
)
cursor = conn.cursor(pymysql.cursors.DictCursor)
cursor.execute('select * from userifo')
user_dict = cursor.fetchall()
return render(request,'index.html',{'user_dict':user_dict})
the fourth step:
uls routing file name in the project folder to add routing
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index),
]