django第5天

虚拟环境安装

'''
1.通过pip3安装虚拟环境:
-- pip3 install virtualenv
2.前往目标文件夹:
-- cd 目标文件夹 (C:\Virtualenv)
3.创建纯净虚拟环境:
-- virtualenv 虚拟环境名 (py3-env1)
了解:创建非纯净环境:
-- virtualenv-clone 本地环境 虚拟环境名
4.终端启动虚拟环境:
-- cd py3-env1\Scripts
-- activate
5.进入虚拟环境下的python开发环境
-- python3
6.关闭虚拟环境:
-- deactivate
7.PyCharm的开发配置
添加:创建项目 -> Project Interpreter -> Existing interpreter -> Virtualenv Environment | System Interpreter -> 目标路径下的python.exe
删除:Setting -> Project -> Project Interpreter -> Show All

伪静态的SEO优化

动态页面:数据内容会发生变化的页面
静态页面:数据内容不会发生变化的页面
针对SEO(搜索引擎优化),静态页面更容易被搜索引擎网站收录
如http://127.0.0.1:8888/index/delete1.html会认为是用固定的html显示的页面(也就是静态页面)
伪静态就是将动态页面伪装成静态页面,容易被搜索引擎网站收录,从而增加搜索概率,提高流量
路由层:
url(r'^article/(?P<id>(\d+)).html/$',views.article,name = 'article')
视图函数层:
def article(request,id):
return render(request,'article.html',{'id':id})
模板层:
index.html
<a href="{% url 'article' 1 %}">第一篇文章</a>
<a href="{% url 'article' 2 %}">第二篇文章</a>
<a href="{% url 'article' 3 %}">第三篇文章</a> article.html
<h1>第{{ id }}篇文章</h1> 执行顺序:
①点击127.0.0.1:8888 进入主页
②点击a标签进入对应的url路径,参数传给了视图函数,视图函数又将参数传给了模板

request对象

1.method:请求方式
2.get请求的参数:GET
3.post请求的参数(本质是从body中取出来):POST
4.body:post提交的数据(不能直接查看)
5.path:请求的路径,不带参数
6.request.get_full_path():请求路径,带参数
7.FILES:文件数据
8.encoding:编码格式
9.META:数据大汇总的字典

FBV与CBV

FBV:function base views 函数方式完成视图响应
CBV:class base views 类方式完成视图响应
视图层:
from django.views import View
from django.shortcuts import HttpResponse class Cbvview(View):
def get(self,request):
return HttpResponse("响应get请求")
def post(self,request):
return HttpResponse("响应post请求")V 路由层:
url('^path/$',views.Cbvview.as_view()) get方法返回get请求的结果
post方法返回post请求的结果

文件上传

模板
<form action='/path/' methode = 'post' enctype = 'multipart/form-data'>#enctype传送二进制数据
{% csrf_token%}
<input type = 'file' name = 'files' multiple>#multiple支持多文件上传
<input type = 'submit' value = "上传">
</form> 视图函数:
files = request.FILES.getlist('files')
for file in files:
with open(file.name,'wb')as f:
for line in file:
f.write(line)
05-11 18:17