MVC思想
![](https://img.haomeiwen.com/i10971896/8dd5a0bc87abfb30.jpg)
设计文件结构
![](https://img.haomeiwen.com/i10971896/2a8ddebd0e83524e.png)
View层级(templates文件夹下)
(1)创建hello.html文件
<!DOCTYPE html>
<html>
<head>
<title>首页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>
<body class="geEditor">
<h1>Hello World</h1>
<h1>{{hello}}</h1>
</body>
</html>
Controller层级(HelloWorld文件下)
(1)在settings.py文件中主要修改DIRS属性,文件的路径设置
'DIRS': [os.path.join(BASE_DIR, 'templates')],
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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',
],
},
},
]
(2)在urls.py文件中配置hello.html文件路径
from django.conf.urls import url
from HelloWorld.view import hello
from . import view
urlpatterns = [
url(r'^hello/$',hello)
]
(3)结果:
![](https://img.haomeiwen.com/i10971896/218db5b05bbda52e.png)
Model层级
(1):在settings.py中配置MySQL连接信息
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'test_hong',
'USER':'root',
'PASSWORD':'123456',
'HOST':'localhost',
'PORT':'3306',
}
}
(2)定义模型
Django规定如果要使用模型,必须创建一个APP
命令创建APP:django-admin startapp TestModel
TestModel文件夹自动生成
![](https://img.haomeiwen.com/i10971896/432f119e4b36d8b4.png)
(3)设置模型信息,修改models.py信息
# models.py
from django.db import models
class Test(models.Model):
name = models.CharField(max_length=20)
1.类名代表了,数据库表名
2:name 代表了字段
3: 数据类型由CharField(相当于varchar)
4:max_length限定参数长度
(4)添加TestModel在settings.py文件中
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'TestModel'
]
(5) 创建表的结构
命令:python manage.py migrate
出现错误
![](https://img.haomeiwen.com/i10971896/cb60e92284eef677.png)
解决方法:
在_init_.py文件中,添加
import pymysql
pymysql.install_as_MySQLdb()
然后 接着运行 python manage.py migrate
结果:
![](https://img.haomeiwen.com/i10971896/54475f5b93014924.png)
数据库结果:
![](https://img.haomeiwen.com/i10971896/85c369c67a1913b8.png)
(6)创建model对象
命令 - python manage.py makemigrations TestModel
结果:
![](https://img.haomeiwen.com/i10971896/50c3deb0dfae94bb.png)
(7)生成表结构
命令 - python manage.py migrate TestModel
![](https://img.haomeiwen.com/i10971896/a30402e88e7d17b2.png)
![](https://img.haomeiwen.com/i10971896/359653cc86a4677b.png)
(8)网页访问,添加数据
在HelloWorld文件夹下,创建testdb.py文件
from django.http import HttpResponse
from TestModel.models import Test
# 数据库操作
def testdb(request):
test1 = Test(name="Test Data")
test1.save()
return HttpResponse('<p>数据添加成功!</p>')
路径引入
from django.conf.urls import url
from HelloWorld.view import hello
from HelloWorld.testdb import testdb
from . import view
urlpatterns = [
url(r'^hello/$',hello),
url(r'^testdb/$',testdb)
]
启动服务,浏览器访问结果:
![](https://img.haomeiwen.com/i10971896/b34325ee9be99bbd.png)
(9)数据库最终插入数据
![](https://img.haomeiwen.com/i10971896/c38d7fbb009fd9d9.png)
网友评论