token&静态文件&媒体文件
1. token
11. 会话技术 2 32. 服务端会话技术 4 53. 它实际上就是手动实现的session 6 74. 实现token 8 9 4.1 在models.py中User类中添加token字段 10 11 class User(models.Model): 12 13 name = models.CharField(max_length=30, unique=True) 14 15 password = models.CharField(max_length=32) 16 17 age = models.IntegerField(default=1) 18 19 token = models.CharField(max_length=32, null=True, blank=True, default='') 20 21 4.2 md5加密 22 23 # md5加密 24 25 def my_md5(password): 26 27 md5 = hashlib.md5() 28 29 md5.update(password.encode('utf-8')) 30 31 return md5.hexdigest() 32 33 4.3 注册用户时,随机生成唯一的token 34 35 token/usertoken, 用户唯一标识 36 37 可以使用 时间+随机数+公司域名+ip信息 或 时间+随机数等方式生成token 38 39 # 生成加密的token 40 41 def generate_token(): 42 43 token = str(time.time()) + str(random.random()) 44 45 return my_md5(token) 46 47 4.4 登录时使用cookie存储token 48 49 # token 50 51 d = datetime.datetime.now() + datetime.timedelta(days=7) # 保存7天 52 53 response.set_cookie('token', res.first().token, expires=d) 54 55 4.5 根据token获取用户信息 56 57 token = request.COOKIES.get('token', '') 58 59 users = User.objects.filter(token=token) 60 61 4.6 退出登录 62 63 response = HttpResponseRedirect(reverse('app:index')) 64 65 # 删除cookie: token 66 67 response.delete_cookie('token') 68 69 return response
2. 用户登录注册
11. 用户注册 2 3 将用户名,用户密码,用户信息,存储到数据库中 4 5 6 72. 用户登陆 8 9 使用用户名,用户密码进行数据库校验 10 11 12 133. 用户信息 14 15 根据用户的唯一标识,去获取用户 16 17 18 194. 数据安全 20 21 服务器的数据对任何人来说都应该是不可见的(不透明) 22 23 可以使用常见的摘要算法对数据进行摘要(md5,sha1) 24 25 如果使用了数据安全,那么就需要在所有数据验证的地方都加上 数据安全
3. 静态文件和媒体文件
1媒体文件:用户上传的文件,叫做media 2 3静态文件:存放在服务器的css,js,image等 叫做static
3.1 在django中使用静态文件
1 1)首先确保django.contrib.staticfiles在 INSTALLED_APPS中 2 3 2)在settings中定义 STATIC_URL 4 5 STATIC_URL = '/static/' 6 7 3)在你app的static目录中存放静态文件,比如: 8 9 my_app/static/my_app/example.jpg. 10 11 4)如果有别的静态资源文件,不在app下的static目录下,可以通过 12 13STATICFILES_DIRS来指定额外的静态文件搜索目录。 14 15 STATICFILES_DIRS = [ 16 17 os.path.join(BASE_DIR, "static"), 18 19 ... 20 21 ] 22 23 5)在模板中使用load标签去加载静态文件 24 25 {% load static %} 26 27 <img src="{% static "my_app/example.jpg" %}" alt="My image"/>
3.2 在django中使用媒体文件
1 1)在settings中配置 MEDIA_ROOT 2 3 MEDIA_ROOT = os.path.join(BASE_DIR, "media")
4. 文件上传
1文件上传要求form表单存在enctype="multipart/form-data"属性,并且提交方法是post。 2 3 <form enctype="multipart/form-data" action="/uploadFile/" method="post"> 4 5 <input type="file" name="myfile" /> 6 7 <br/> 8 9 <input type="submit" value="upload"/> 10 11 </form> 12 13最简单的文件上传: 14 15 def file_upload(request): 16 17 if request.method == 'POST': 18 19 # 获取上传的文件,如果没有文件,则默认为None 20 21 myFile = request.FILES.get('myfile', None) 22 23 if not myFile: 24 25 return HttpResponse("no files for upload") 26 27 28 29 file_path = os.path.join(settings.MEDIA_ROOT, '1.jpg') 30 31 with open(file_path, 'ab') as fp: 32 33 for part in myFile.chunks(): 34 35 fp.write(part) 36 37 return HttpResponse("上传成功!") 38 39 40 41 else: 42 43 return render(request, 'index.html')
5. 多文件上传
1多文件上传和单文件上传类似 2 3 1.需要在模板文件的form表单input中添加multiple 4 5 2.后台获取时使用request.FILES.getlist('myfile', None) 6 7def file_upload2(request): 8 9 if request.method == 'POST': 10 11 # 获取上传的文件,如果没有文件,则默认为None 12 13 myFiles = request.FILES.getlist('myfile', None) 14 15 for myFile in myFiles: 16 17 if not myFile: 18 19 return HttpResponse("no files for upload") 20 21 file_path = os.path.join(settings.MEDIA_ROOT, myFile.name) 22 23 with open(file_path, 'ab') as fp: 24 25 for part in myFile.chunks(): 26 27 fp.write(part) 28 29 return HttpResponse("上传成功!") 30 31 32 33 else: 34 35 return render(request, 'index.html')
6.分页
6.1 分页工具
1django提供了分页的工具,存在于django.core中 2 3 Paginator : 数据分页工具 4 5 Page : 具体的某一页面 6 7 8 9导入Paginator: 10 11 from django.core.paginator import Paginator 12 13 14 15Paginator: 16 17对象创建: 18 19 Paginator(数据集,每一页数据数) 20 21属性: 22 23 count:对象总数 24 25 num_pages:页面总数 26 27 page_range: 页码列表,从1开始 28 29方法: 30 31 page(整数): 获得一个page对象 32 33 34 35常见错误: 36 37 InvalidPage:page()传递无效页码 38 39 PageNotAnInteger:page()传递的不是整数 40 41 Empty:page()传递的值有效,但是没有数据 42 43 44 45Page: 46 47 对象获得,通过Paginator的page()方法获得 48 49属性: 50 51 object_list: 当前页面上所有的数据对象 52 53 number: 当前页的页码值 54 55 paginator: 当前page关联的Paginator对象 56 57方法: 58 59 has_next() :判断是否有下一页 60 61 has_previous():判断是否有上一页 62 63 has_other_pages():判断是否有上一页或下一页 64 65 next_page_number():返回下一页的页码 66 67 previous_page_number():返回上一页的页码 68 69 len():返回当前页的数据的个数