Liupeng
Jun 27, 2020
简介
Django是一个什么东西?
我现在没有搞明白!
建立一个Django项目
编译器
-
PyCharm
Start
Create New Project
Setting Project
Over
Run
Effect
File
Mainly introduce the role of file directory.
asgi.py
条件限制,我没有详细了解过,If I learn to update it later.
setting.py
项目配置文件
- 配置功能
- 国际化配置(语言和时区)
- 数据库配置
urls.py
路径配置文件
wigs.py
条件限制,我没有详细了解过,If I learn to update it later.
db.sqlite3
自动弹出的数据库,默认使用sqlite。
manage.py
条件限制,我没有详细了解过,If I learn to update it later.
Django Project cutover Databases
Databases
- MySQL
How to Cutover Databases
update setting.py
path
yourProject/yourProject/settings.py
ordCode
newCode
tall Project your cutover databases
path
yourProject/yourProject/init.py
add code
import pymysql
pymysql.install_as_MySQLdb()
Registered background administrator
Open Terminaal
input command line code
python manage.py migrate
python manage.py createsuperuser
Create App Or Function
Maybe you should know something about MVC or MVT
create file by django
input command line code
python manage.py startapp your_app_name
‘your_app_name’ as ‘card’:
What is the role of the file?
__init__.py
不知道,🤷♂️!
admin.py
设置管理员对该功能的管理
apps.py
不知道,🤷♂️!
models.py
数据实体,与数据库交互(ORM)
test.py
测试
views.py
视图(返回数据还是页面)
配置路由
在你的yourproject/book/views.py中写一个方法
def sayHelloAt20200626(request):
return HttpResponse("Hello World")
在urls.py中配置
浏览器访问
Communicate with database
Create Model
Path
yourProject/yourApp/models.py
Code
from django.db import models
# Create your models here.
# Book
class Book(models.Model):
'''Book model class'''
# 字符串
# max_length : 最大长度
title = models.CharField(max_length=40)
# pub date
# date type
pub_date = models.DateField()
def __str__(self):
return self.title
Create initial file
Open Terminal
input command line code
python manage.py makemigrations
initial file ctrate table in databases
Open Terminal
input command line code
python manage.py migrate
Databaes
数据增删改查
Create
e = Entry()
e.name = "root"
e.save()
Select
e = Entry.objects.get(id=2)
Add
e.save()
Update
e.name = "newName"
e.save()
Show Data In Web Page
Create Html File
path
yourProject/templates/book/index.html
code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<h1>
Welcome! My friend!
</h1>
<hr>
<ol>
</ol>
</body>
</html>
create view function
path
yourProject/book/views.py
Code1
def indexPage(request):
# 记载模板文件,生产模板对象
template = loader.get_template('book/index.html')
# 给定模板上下文,给模板文件传递数据
context = {'numbers': list(range(1, 10))}
# 模板渲染:产生标准的html文档
res_html = template.render(context, request)
# 返回
return HttpResponse(res_html)
code2
def indexPage(request):
return render(request, "book/index.html", {'numbers': list(range(1, 10))})
Setting Path or Url
App
path
yourProject/book/urls.py
code
from django.contrib import admin
from django.urls import path, include
from django.conf.urls import url
from book.views import sayHelloAt20200626
from book.views import indexPage
from book.views import showBooks
from book.views import bookDetail
urlpatterns = [
url(r'^sayHelloAt20200626$', sayHelloAt20200626),
url(r'^indexPage$', indexPage),
]
Project
path
yourProject/yourProject/urls.py
code
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('book.urls'))
]