manage.py命令行工具
__init__.py初始化文件
settings.py项目的配置文件
urls.py项目的URL设置,网站的地址信息
wsgi.py是python应用与web服务器之间的接口,用于Django项目在服务器上的部署和上线,一般不需要修改
migrations用于数据库数据的迁移
__init__.py初始化文件
admin.py当前APP的后台管理系统
apps.py当前APP的配置信息
models.py定义映射类关联数据库,实现数据持久化
tests.py自动化测试的模块
views.py逻辑处理模块
在settings.py文件中,
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
STATIC_URL = '/static/'
STATICFILES_DIRS=[os.path.join(BASE_DIR,'public_static'),
os.path.join(BASE_DIR,'index/index_static'),]
/login/ login
def login(request):
request.method
request.POST ->请求体
request.GET ->请求头中的URL
request.POST.getlist
return HttpResponse() -->返回字符串
return render(request,'login.html',{...}) -->函数里的参数值,模板,渲染
return redirect('要跳转的网址')
GET请求->只有request.GET中有值
POST请求->GET和POST都可以获得值
for,if,列表索引.
login.html
{{name}}
def login(request):
return render(request,'login.html',{'name':'alex'})
学员管理:
表:
班级 学生 老师
单表操作:
增删改查
多对多操作
增,删,改,查,
js阻止默认事件的发生
location.reload()重新加载
HttpReponse(json.dumps())将json格式转化为字符串
JSON.parse()将字符串转化为json
jQuery事件阻止默认事件发生
$(‘#addModal’).clicak(function(){
return flase;
})
模板渲染是在后台执行
$.ajax({
url:'',
type:'',
data:{},
datatype:'JSON',#把arg转化为json对象
success:function(arg){}
})
请求头到达后台,后台提取url进行路由匹配,然后找到相应的函数(模板+数据渲染),最后再返回给用户(响应头+响应体)
一个包含CSS和JS的一个代码库
(1)样式
(2)响应式@media关键字
导航条
栅格
(3)当鼠标移动到XX样式的标签上时,其子标签.g应用到以下属性
.xx:hover .g{}
1.页面继承
母版:存放所有页面公用
{% block css %} {%endblock%}
...
{% block s1 %} {%endblock%}
...
{% block js %} {%endblock%}
子板:继承母版,自定义当前页面私有的东西
{% extends "layout.html "%}
{% block s1 %} fff
{%endblock%}
{% block s2 %} ffffff
{%endblock%}
2、函数-》自动执行
'userinfo':{'k1':'v1','k2':'v2'}
打印字典所有的key
{% for k,v in userinfo.items %}
{{k}}-{{v}}
{% endfor %}}
3、自定义函数
a.在app中创建templatetags模块
b.创建任意.py文件,例如,xx.py
#此处放置自定义的函数
from django import template
register=template.Library()
#自定义一个函数
@register.filter
def my_upper(value):
return value.upper()
def my_bool(value):
return False
@register.simple_tag
def my_lower(value):
return value.lower()
c.在使用自定义simple_tag的html文件中导入之前创建的.py文件名
#在使用simple_tag的HTML文件的最上面导入
{% load xx %}
d.使用smple_tag
#filter
#-最多两个参数,方式: {{第一个参数|函数名称:"第二个参数"}}
{{name|my_upper}}
#tag
#无限制参数: {% 函数名 参数 参数%}
{% my_lower "ALEX" %}
#filter可以做条件判断
{% if name|my_bool %}
真
{%else%}
假
{% endif %}
e.在settings中配置当前app.不然django无法找到自定义的simple_tag
4、include导入
(可以导入多个,母版只能继承一次)
导入小组件
pub.html
特别漂亮的组件
标题:{{ name }}
内容:{{ name }}
test.html
{% include 'pub.html' %}
{% include 'pub.html' %}
{% include 'pub.html' %}
-cookie: a.保存在浏览器端“键值对”,设置超时时间
b.服务端可以向用户浏览器端写cookie
c.客户端每次请求时,会携带cookie去
- 发送Http请求时,在请求头中携带当前所有可访问的cookie
- 响应头
- 写cookie
@xzxx
def index(request):
obj = HttpResponse('...')
obj.set_cookie(.....)
request.COOKIES.get(...)
obj.set_signed_cookie(.....)
request.get_signed_cookie(....)
set_cookie
set_cookie
key,
value='',
max_age=None,
expires=None,
path='/',
domain=None,
secure=False, Https
httponly=False 只能自Http请求中传入,js代码无法获取到
cookie签名:
obj.set_signed_cookie('ticket',"123123",salt='jjjjjj')
#去请求的cookie中找凭证
request.get_signed_cookie('ticket',salt='jjjjjj')
- 自定义cookie签名
- 装饰器装饰views中的函数
django-admin startproject mysite
cd mysite
python manage.py starapp app01
project
- app01
- admin Django自带后台管理相关配置
- modal 写类,根据类创建数据库表
- test 单元测试
- views 业务处理
- app02
- app03
http://127.0.0.1:8080/edit/?nid=fff
url(r'^edit/',views.edit),
def edit(request):
request.GET.get('nid')
return HttpResponse('...')
http://127.0.0.1:8080/edit/fff
url(r'^edit/(\w+)/',views.edit),
def edit(request,a1):
return HttpResponse('...')
http://127.0.0.1:8080/edit/fff/sds
url(r'^edit/(\w+)/(\w+)/',views.edit)
def edit(request,a1,a2):
return HttpResponse('...')
url(r'^edit/(?P\w+)/(?P\w+)/',views.edit)
def edit(request,a1,a2):
return HttpResponse('...')
终止符
http://127.0.0.1:8080/edit/
url(r'^edit/',views.edit),
http://127.0.0.1:8080/edit(这样的方式后面有没有/)
url(r'^edit$',views.edit),
伪静态
url(r'^edit/(\w+).html$',views.edit),
路由别名反向生成url
url(r'^index/',views.index,name='n1'),
name根据名字反生成url
def index(request):
v=reverse('n1')
print(v)#/index/
url(r'^index/(\d+)/',views.index,name='n1'),
def index(request):
v=reverse('n1',args=(1,))
print(v)#/index/1/
url(r'^index/(?P\d+)/',views.index,name='n1'),
from django.urls import reverse
v = reverse('n1',kwargs={'a1':1111})
print(v)
url(r'^login/', views.login,name='m1')
url -> 函数
a. /login/ -> def login
b. /add-user/(\d+)/ -> def add_user(request,a1)
c. /add-user/(?P\d+)/ -> def add_user(request,a1)
PS:
终止符:
^edit$
伪静态
url(r'^edit/(\w+).html$', views.edit),
d. 路由分发
urls.py
url(r'^app01/', include('app01.urls')),
url(r'^app02/',include('app02.urls')),
app01.urls.py
url(r'^index.html$', views.index),
如果用户所输入的地址不存在,可以设置一个默认的跳转页面
def default(request):
return HttpResponse('错误')
url(r'^',default),
e. /add-user/(\d+)/ -> def add_user(request,a1) name=n1
根据名称可以反向生成URL
1. 在Python代码中
from django.urls import reverse
v = reverse('n1',kwargs={'a1':1111})
print(v)
2.
url(r'^login/', views.login,name='m1')
{% url "m1" %}
ORM利用pymysql第三方工具连接数据库
默认:
SQLlite
MySQL:
mysql -> MySQLDB(修改django默认连接mySQL方式)
Http请求:
url -> 视图(模板+数据)
步骤:
1. 创建数据库
2. settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'s4day70db',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': 3306,
}
}
3. __init__.py
import pymysql
pymysql.install_as_MySQLdb()
4. models.py
from django.db import models
class UserInfo(models.Model):
nid=models.BigAutoField(primary_key=True)
username=models.CharField(max_length=32) password=models.CharField(max_length=64)
5.settings.py注册APP
在installed_apps中加入app的名字
6.创建数据表
命令:
python manage.py makemigrations
python manage.py migrate
7. 创建表
from django.db import models
from django.db import models
class UserGroup(models.Model):
"""
部门
"""
title = models.CharField(max_length=32)
"""
增
models.UserGroup.objects.create(title='销售部')
删
models.UserGroup.objects.filter(id=2).delete()
改
models.UserGroup.objects.filter(id=2).update(title='公关部')
查
group_list = models.UserGroup.objects.all()
group_list = models.UserGroup.objects.filter(id=1)
group_list = models.UserGroup.objects.filter(id__gt=1)
group_list = models.UserGroup.objects.filter(id__lt=1)
"""
# Create your models here.
class UserInfo(models.Model):
"""
员工
"""
nid = models.BigAutoField(primary_key=True)
username = models.CharField(max_length=32)
password = models.CharField(max_length=64)
#age=models.IntegerField(null=True)
age = models.IntegerField(default=1)
ug = models.ForeignKey("UserGroup", null=True,on_delete=models.DO_NOTHING)
#models.py
from django.db import models
class UserType(models.Model):
'''用户类型'''
title=models.charField(max_length=32)
class UserInfo(models.Model):
'''用户表'''
name=models.charField(max_length=16)
age=models.IntegerField()
ut=models.ForeignKey('UserType')
#views.py
from django.shortcuts import render,HttpResponse
from app01 import models
from django.core.paginator import Paginator,Page,PageNotAnInteger,EmptyPage
def test(request):
# 创建数据
# models.UserType.objects.create(title='普通用户')
# models.UserType.objects.create(title='二逼用户')
# models.UserType.objects.create(title='牛逼用户')
# models.UserInfo.objects.create(name='方少伟',age=18,ut_id=1)
# models.UserInfo.objects.create(name='由秦兵',age=18,ut_id=2)
# models.UserInfo.objects.create(name='刘庚',age=18,ut_id=2)
# models.UserInfo.objects.create(name='陈涛',age=18,ut_id=3)
# models.UserInfo.objects.create(name='王者',age=18,ut_id=3)
# models.UserInfo.objects.create(name='杨涵',age=18,ut_id=1)
# 获取
# QuerySet[obj,obj,obj
# result = models.UserInfo.objects.all()
# for obj in result:
# print(obj.name,obj.age,obj.ut_id,obj.ut.title)
# UserInfo,ut是FK字段 - 正向操作 PS: 一个用户只有一个用户类型
# obj = models.UserInfo.objects.all().first()
# print(obj.name,obj.age,obj.ut.title)
# UserType, 表名小写_set.all() - 反向操作 PS: 一个用户类型下可以有很多用户
# obj = models.UserType.objects.all().first()
# print('用户类型',obj.id,obj.title)
# for row in obj.userinfo_set.all():
# print(row.name,row.age)
models.UserInfo.objects.filter(id_gt=1)
models.UserInfo.objects.exclude(id=1)
user_list=models.UserInfo.objects.all().order_by('-id','name')
from django.db.models import Count,Sum,Max,Min
v=models.UserInfo.objects.values('ut_id').annotate(xxxx=Count('id'))
print(v.query)
v=models.UserInfo.objects.values('ut_id').annotate(xxxx=Count('id')).filter(xxxx__gt=2)
v=models.UserInfo.objects.filter(id__gt=2).values('ut_id').annotate(xxxx=Count('id')).filter(xxxx__gt=2)
print(v.query)
from django.db.models import F,Q
models.UserInfo.objects.all().update(age=F('age')+1)
q1=Q()
q1.connector='OR'
q1.children.append(('id_gt',1))
q1.children.append(('id', 10))
q2=Q()
q2.connector='OR'
q2.children.append(('c1',1))
q2.children.append(('c2',2))
q3=Q()
q3.connector='AND'
q3.children.append(('id',1))
q3.children.append(('id',2))
q2.add(q3,'OR')
con=Q()
con.add(q1,'AND')
con.add(q2,'AND')
models.UserInfo.objects.filter(con)
models.UserInfo.objects.extra(self, select=None, where=None, params=None, tables=None, order_by=None, select_params=None)
select …from 表
select
select_params=None
select * from 表 where…
where
params=None
select * from 表,…
tables
select * from 表 order by…
order_by=None
models.UserInfo.objects.extra(
select={'newid':'select count(1) from app01_usertype where id>%s'},
select_params=[1,],
where=['age>%s'],
params=[18,],
order_by=['-age'],
tables=['app01_usertype']
)
"""
select
app01_userinfo.id,
(select count(1) from app01_usertype where id>1) as newid
from app01_userinfo,app01_usertype
where
app01_userinfo.age > 18
order by
app01_userinfo.age desc
"""
v=models.UserInfo.objects.all().extra(
select={
'n':'select count(1) from app01_usertype where id=%s or id=%s',
'm':'select count(1) from app01_usertype where id=%s or id=%s',
},
select_params=[1,2,3,4]
)
#where中元素是通过列表连接的
models.UserInfo.objects.extra(
where=['id=1 or id=%s','name=%s'],
params=[1,"alex"]
)
models.UserInfo.objects.extra(
tables=['app01_usertype'],
where=['app01_usertype.id=app01_userinfo.ut_id']
)
"""select * from app01_userinfo,app01_usertype"""
"""select * from app01_userinfo,app01_usertype where app01_usertype.id=app01_userinfo.ut_id"""
from django.db import connection,connections
cursor=connection.cursor()#默认连接default数据库
#default为默认的连接数据库的名字
cursor=connections['default'].cursor()
#也可以连接其他的数据库
cursor=connections['db2'].cursor()
cursor.execute("""SELECT * from auth_user where id = %s""", [1])
row = cursor.fetchone()
row = cursor.fetchall()
http://www.cnblogs.com/wupeiqi/articles/6216618.html
1.增
create(name=‘xx’,age=‘xx’…)
dic={name’'xxx,age}
create(**dic)
2.更新
models.xx.objects.filter(id=1).update(a=1,b=2)
models.xx.objects.filter(id=1).update(**dic)
3、查询
models.xx.objects.filter(id=1,xxx)
models.xx.objects.filter(**{‘id’:1,‘name’:‘alex’})
Django帮助数据转换->字典 =》Form组件(用户请求规则验证+数据字典)
select_related:查询主动做连表(但是相对于prefetch_related慢)
q=models.UserInfo.objects.all()
#select * from userinfo
for row in q:
print(row.name,row.ut.title)
#这种查询表多次,较满
q=models.UserInfo.objects.all().select_related('ut','gp')
#select * from userinfo inner join usertype on ...
for row in q:
print(row.name,row.ut.title)
prefetch_related:不做连表,做多次查询
q=models.UserInfo.objects.all().prefetch_related('ut')
#select * from userinfo;
#select * from usertype where id in [2,4]
for row in q:
print(row.id,row.ut.title)
1.manyToManyField自动创建
2.自定义关系表
3.两者结合
models.py
class Boy(models.Model):
name=models.CharField(max_length=32)
class Girl(models.Model):
nick=models.CharFiled(max_length=32)
class Love(models.Model):
b=models.ForeignKey('Boy')
g=models.ForeignKey('Girl')
#联合唯一索引
class Meta:
unique_together=[
('b','g'),
]
#创建表
python manage.py makemigrations
python manage.py migrate
views.py
#多对多
objs=[
models.Boy(name='aa'),
models.Boy(name='bb'),
models.Boy(name='cc'),
models.Boy(name='dd'),
models.Boy(name='ee'),
]
models.Boy.objects.bulk_create(objs,5)
objss=[
models.Girl(nick='小花'),
models.Girl(nick='小斯'),
models.Girl(nick='小萌'),
models.Girl(nick='小心'),
models.Girl(nick='小小'),
]
models.Girl.objects.bulk_create(objss,5)
models.Love.objects.create(b_id=1,g_id=1)
models.Love.objects.create(b_id=1,g_id=4)
models.Love.objects.create(b_id=2,g_id=4)
models.Love.objects.create(b_id=2,g_id=2)
#1.和aa有关系的女生
#方法一
obj=models.Boy.objects.filter(name='aa').first()
love_list=obj.love_set.all()
for row in love_list:
print(row.g.nick)
#方法二
love_list=models.Love.objects.filter(b__name='aa')
for row in live_list:
print(row.g.nick)
#方法三
love_list=models.Love.objects.filter(b__name='aa').values('g__nick')
for item in love_list:
print(item['g__nick'])
love_list=models.Love.objects.filter(b__name='aa').select_rated('g')
for obj in love_list:
print(obj.g.nick)
#
Django也提供自动创建第三张表,但是无法直接对第三张表操作,但是可以间接操作,且只能生成三列
class Boy(models.Model):
name=models.CharField(max_length=32)
m=models.ManyToManyField('Girl')
class Girl(models.Model):
nick=models.CharFiled(max_length=32)
#创建表
python manage.py makemigrations
python manage.py migrate
obj=models.Boy.objects.filter(name='aa').first()
print(obj.id,obj.name)
#增
#obj.m.add(3)
#obj.m.add(2,4)
#obj.m.add(*[1,])
#删
obj.m.remove(1)
obj.m.remove(2,3)
#改
obj.m.set([1,])
#获取
q=obj.m.all()#得到[女生对象]
#获取aa联系的女生
obj=models.Boy.objects.filter(name='aa').first()
#girl_list=obj.m.all()
girl_list=obj.m.filter(nick='小小')
obj=models.Boy.objects.filter(name='aa').first()
obj.m.clear()
obj=models.Girl.objects.filter(nick='小小').first()
v=obj.boy_set.all()
#m=models.ManyToManyField(‘Girl’,through=“Love”,through_felds=(‘b’,‘g’,))
同时自动创建和用自己定义的表;查询和清空的功能
FBV:就是在视图里使用函数处理请求
CBV:就是在视图里使用类处理请求(使用class-based views)
from django.http import HttpResponse
from django.views import View
class MyView(View):
def get(self,request):
return HttpResponse('ok')
class-based view提供了一个as_view()静态方法,调用这个方法,会创建一个类的实例,然后通过实例调用dispatch()方法,dispatch()会根据request的method的不同调用相应的方法来处理request
#urls.py
from app01 import views
urlpatterns=[
url(r'^login.html$', views.Login.as_view()),
]
1.分批获取数据
models.UserInfo.objects.all()[0:10]
models.UserInfo.objects.all()[10:20]
2.Django自带
from django.core.paginator import Paginator,Page,PageNotAnInteger,EmptyPage
def index(request):
current_page=request.GET.get('page')
user_list=models.UserInfo.objects.all()
paginator=Paginator(user_list,10)
# per_page: 每页显示条目数量
# count: 数据总个数
# num_pages:总页数
# page_range:总页数的索引范围,如: (1,10),(1,200)
# page: page对象
try:
posts=paginator.page(current_page)
except PageNotAnInteger as e:
posts=paginator.page(1)
except EmptyPage as e:
posts=paginator.page(1)
return render(request,'index.html',{'posts':posts})
# has_next 是否有下一页
# next_page_number 下一页页码
# has_previous 是否有上一页
# previous_page_number 上一页页码
# object_list 分页之后的数据列表
# number 当前页
# paginator paginator对象
用户列表
{%for row in posts.object_list %}
- {{row.name}}
{% endfor %}
3.自定义分页组件
from utils.pager import PageInfo
def custom(request):
# 表示用户当前想要访问的页码: 8
all_count = models.UserInfo.objects.all().count()
page_info = PageInfo(request.GET.get('page'),all_count,10,'/custom.html',11)
user_list = models.UserInfo.objects.all()[page_info.start():page_info.end()]
return render(request,'custom.html',{'user_list':user_list,'page_info':page_info})
#paper.py
class PageInfo(object):
def __init__(self,current_page,all_count,per_page,base_url,show_page=11):
"""
:param current_page:
:param all_count: 数据库总行数
:param per_page: 每页显示函数
:return:
"""
try:
self.current_page = int(current_page)
except Exception as e:
self.current_page = 1
self.per_page = per_page
a,b = divmod(all_count,per_page)
if b:
a = a +1
self.all_pager = a
self.show_page = show_page
self.base_url = base_url
def start(self):
return (self.current_page-1) * self.per_page
def end(self):
return self.current_page * self.per_page
def pager(self):
# v = "12"
# return v
page_list = []
half = int((self.show_page-1)/2)
# 如果数据总页数 < 11
if self.all_pager < self.show_page:
begin = 1
stop = self.all_pager + 1
# 如果数据总页数 > 11
else:
# 如果当前页 <=5,永远显示1,11
if self.current_page <= half:
begin = 1
stop = self.show_page + 1
else:
if self.current_page + half > self.all_pager:
begin = self.all_pager - self.show_page + 1
stop = self.all_pager + 1
else:
begin = self.current_page - half
stop = self.current_page + half + 1
if self.current_page <= 1:
prev = "上一页 "
else:
prev = "上一页 " %(self.base_url,self.current_page-1,)
page_list.append(prev)
for i in range(begin,stop):
if i == self.current_page:
temp = "%s " %(self.base_url,i,i,)
else:
temp = "%s " %(self.base_url,i,i,)
page_list.append(temp)
if self.current_page >= self.all_pager:
nex = "下一页 "
else:
nex = "下一页 " %(self.base_url,self.current_page+1,)
page_list.append(nex)
return ''.join(page_list)
#custom.html
用户列表
{% for row in user_list %}
- {{ row.name }}
{% endfor %}
python manage.py makemigrations
python manage.py migrate
class UserGroup(models.Model):
“”"
部门 3
“”"
title = models.CharField(max_length=32)
class UserInfo(models.Model):
“”"
员工4
“”"
nid = models.BigAutoField(primary_key=True)
user = models.CharField(max_length=32)
password = models.CharField(max_length=64)
age = models.IntegerField(default=1)
ug = models.ForeignKey(“UserGroup”,null=True)
字符串:
EmailField(CharField)
IPAddressField(Field)
URLField(CharField)
SlugField(Field)
UUIDField(Field)
FilePathField(Field)
FileField(Field)
ImageField(FileField)
CommaSeparatedIntegerField(CharField)
时间类
models.DateTimeField(null=True)
数字
num=models.InterField()
num=models.FloatField()
num=models.DecimalField(max_digits=30,decimal_places=10)
枚举:
color_list=(
(1,'黑色'),
(2,'白色'),
(3,'蓝色')
)
color=models.IntegerField(choices=color_list)
字段参数:
null=True,
default='1111',
db_index=True,
unique=True
class Meta:
unique_together=(
('email','ctime'),
)
index_together=(
('email','ctime'),
)
跨表:
正向
1.q=UserInfo.objects.all().first()
2.UserInfo.objects.values(‘nid’,‘ug_id’)
3.UserInfo.objects.values_list(‘nid’,‘ug_id’,‘ug_title’)
反向
1.obj=UserGroup.objects.all().first()
result=obj.userinfo_set.all()[userinfo对象,userinfo对象,]
2.小写的表名
v = UserGroup.objects.values(‘id’,‘title’)
v = UserGroup.objects.values(‘id’,‘title’,‘小写的表名称’)
v = UserGroup.objects.values(‘id’,‘title’,‘小写的表名称__age’)
前面的所有数据都会显示
其他
UserInfo.objects.all()
UserInfo.objects.filter(id=1,id=2)
UserInfo.objects.all().first()
UserInfo.objects.all().count()
UserInfo.objects.all().delete()
UserInfo.objects.all().update()
UserInfo.objects.all()[1:19]
跨表:
正向:
UserInfo.filter(‘ut__title’=’ ').values()
反向
UserGroup.filter(‘userinfo__title’=’ ').values()
a、基本应用
form表单中添加
{% csrf_token %}
b、全站禁用
#'django.middleware.csrf.CsrfViewMiddleware'
c、局部禁用
django.middleware.csrf.CsrfViewMiddleware
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def csrf1(request):
if request.method=='GET':
return render(request,'csrf1.html')
else:
return HttpResponse('ok')
d、局部使用
#'django.middleware.csrf.CsrfViewMiddleware'
from django.views.decorators.csrf import csrf_exempt,csrf_protect
@csrf_protect
e、特殊CBV
from django.views import View
from django.utils.decorators import method_decorator
@method_decorator(csrf_protect,name='dispatch')
class Foo(View):
def get(self,request):
pass
def post(self,request):
pass
CBV中添加装饰器
PS:CBV中添加装饰器
def wrapper(func):
def inner(*args,**kwargs):
return func(*args,**kwargs)
return inner
# 1. 指定方法上添加装饰器
# class Foo(View):
#
# @method_decorator(wrapper)
# def get(self,request):
# pass
#
# def post(self,request):
# pass
# 2. 在类上添加
# @method_decorator(wrapper,name='dispatch')
# class Foo(View):
#
# def get(self,request):
# pass
#
# def post(self,request):
# pass
Ajax提交数据时候,携带CSRF
-data
-cookie中获取,添加到请求头
a.放置在data中携带
cookie:保存在客户端浏览器上的键值对
session: 保存在服务端的数据(本质是键值对)
应用:依赖cookie
作用:保持会话(web网站)
好处:敏感信息不会直接给客户端
new_user = models.User.objects.create()
new_user.name = username
new_user.password = hash_code(password1) # 使用加密密码
new_user.email = email
new_user.sex = sex
new_user.save()
def updateinfo(request):
if request.method == 'POST':
# img = request.FILES.get('photo')
# user = request.FILES.get('photo').name
new_img = models.mypicture(
photo=request.FILES.get('photo'), # 拿到图片
user=request.FILES.get('photo').name # 拿到图片的名字
)
new_img.save() # 保存图片
return HttpResponse('上传成功!')
return render(request, 'login/lianxi.html')