一、MVT模式
M全拼为Model,负责和数据库交互,进行数据处理。
V全拼为View,接收请求,进行业务处理,返回响应。
T全拼为Template,负责封装构造要返回的html。
V全拼为View,接收请求,进行业务处理,返回响应。
T全拼为Template,负责封装构造要返回的html。
二、注册功能(连接数据库)
#### student/models.py中创建Stu模型类
```
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Stu(models.Model):
sname = models.CharField(max_length=20,unique=True)
spwd = models.CharField(max_length=20,unique=True)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Stu(models.Model):
sname = models.CharField(max_length=20,unique=True)
spwd = models.CharField(max_length=20,unique=True)
```
#### 创建数据库表
```
#创建当前应用的迁移文件
python manage.py makemigrations student
```
#创建当前应用的迁移文件
python manage.py makemigrations student
#生成数据库表
python manage.py migrate
python manage.py migrate
#查看迁移文件生成的SQL语句
python manage.py sqlmigrate student 0001
python manage.py sqlmigrate student 0001
```
#### 配置URL(student/urls.py)
```
#coding=utf-8
from django.conf.urls import url
import views
urlpatterns = [
url(r'^$',views.login_view),
url(r'^login/',views.to_login_view),
url(r'^register/',views.register_view),
]
#coding=utf-8
from django.conf.urls import url
import views
urlpatterns = [
url(r'^$',views.login_view),
url(r'^login/',views.to_login_view),
url(r'^register/',views.register_view),
]
```
#### 配置视图函数(student/views.py)
```
#注册功能
from models import *
def register_view(request):
# print request.method
if request.method=='GET':
return render(request,'register.html')
else:
#获取请求参数
uname = request.POST.get('uname','')
pwd = request.POST.get('pwd','')
#非空判断
if uname and pwd :
stu = Stu(sname=uname,spwd=pwd)
stu.save()
return HttpResponse('注册成功!')
return HttpResponse('注册失败!')
```
三、admin.py操作数据库
#### 编辑admin.py(student/admin.py)
```
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from models import *
# Register your models here.
admin.site.register(Stu)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from models import *
# Register your models here.
admin.site.register(Stu)
```
#### 创建账户密码
```
python manage.py createsuperuser
```
#### 重启服务器
#### 浏览器访问后台管理页面
```
http://127.0.0.1:8000/admin/
http://127.0.0.1:8000/admin/
```
#### 后台方式操作数据库
#### 修改settings.py文件后台页面显示中文
```
LANGUAGE_CODE = 'en-us'
```
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
修改为:
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
```
```
#### 修改models.py文件后台显示对象属性值
```
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Stu(models.Model):
sname = models.CharField(max_length=20,unique=True)
spwd = models.CharField(max_length=20,unique=True)
sname = models.CharField(max_length=20,unique=True)
spwd = models.CharField(max_length=20,unique=True)
def __str__(self):
return u'Stu:%s,%s'%(self.sname,self.spwd)
return u'Stu:%s,%s'%(self.sname,self.spwd)
```
四、登录功能(连接MySQL数据库)#### 安装MySQL-python==1.2.5库
```
#方式1:
运行窗口:
pip install wheel
pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
#方式2:
pycharm/settings/project interpreter中添加库
#方式3:
pip2.7 install MySQL-python==1.2.5
```
#方式1:
运行窗口:
pip install wheel
pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
#方式2:
pycharm/settings/project interpreter中添加库
#方式3:
pip2.7 install MySQL-python==1.2.5
```
#### 修改settings.py文件
```
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',#数据库连接器
'NAME': 'logindemo',#数据库名称
'HOST':'127.0.0.1',#数据库主机地址
'PORT':'3306',#数据库端口
'USER':'root',#数据库用户名
'PASSWORD':'123456'#数据库密码
}
}
```
#### 配置模型类(student/models.py)
```
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Stu(models.Model):
sname = models.CharField(max_length=20,unique=True)
spwd = models.CharField(max_length=20,unique=True)
sname = models.CharField(max_length=20,unique=True)
spwd = models.CharField(max_length=20,unique=True)
def __str__(self):
return u'Stu:%s,%s'%(self.sname,self.spwd)
return u'Stu:%s,%s'%(self.sname,self.spwd)
```
#### 生成数据库表
```
#创建当前应用的迁移文件
python manage.py makemigrations student
#生成数据库表
python manage.py migrate
python manage.py migrate
```
#### 配置URL
```
from django.conf.urls import url, include
from django.contrib import admin
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^student/', include('student.urls')),
]
url(r'^admin/', admin.site.urls),
url(r'^student/', include('student.urls')),
]
```
```
#coding=utf-8
#coding=utf-8
from django.conf.urls import url
import views
import views
urlpatterns = [
url(r'^$',views.login_view),
url(r'^login/',views.to_login_view)
]
url(r'^$',views.login_view),
url(r'^login/',views.to_login_view)
]
```
#### 配置函数视图
```
#处理登录功能
def doLogin_view(request):
#接收请求参数
uname = request.POST.get('uname','')
pwd = request.POST.get('pwd','')
def doLogin_view(request):
#接收请求参数
uname = request.POST.get('uname','')
pwd = request.POST.get('pwd','')
#判断是否登录成功
count = Stu.objects.filter(sname=uname,spwd=pwd).count()
count = Stu.objects.filter(sname=uname,spwd=pwd).count()
if count==1:
return HttpResponse('登录成功!')
else:
return HttpResponse('登录失败!')
return HttpResponse('登录成功!')
else:
return HttpResponse('登录失败!')
```
五、数据库表逆向模型类:
输入的命令是:python manage.py inspectdb>应用名/models.py
六、分页中需要导入的包:from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
七、在views页下进行对象的操作,查询、增删改查等,底层都是操作sql,通过如下方式可以查看
#### 显示ORM底层生成SQL
```
def showsql():
from django.db import connection
queries = connection.queries
print queries[-1]['sql']
def showsql():
from django.db import connection
queries = connection.queries
print queries[-1]['sql']
```