django4 虚拟环境(不太重要)

虚拟环境安装

'''
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(搜索引擎优化),静态页面更容易被搜索引擎网站收录

伪静态就是讲动态页面伪装成静态页面,容易被搜索引擎网站收录,从而增加搜索概率,提高流量

路由层:
url('^index/$', views.index),
url('^article/(?P<id>(\d+)).html/$', views.article, name='article')

视图函数层:
def index(request):
    return render(request, 'index.html')
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>
'''

request对象

'''
1. method: 请求方式
2. GET: get请求的参数
3. POST: post请求的参数(本质是从bdoy中取出来)
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 类方式完成视图响应
'''

'''CBV完成视图响应
视图层:
from django.shortcuts import HttpResponse
from django.views import View
class CBVView(View):
    def get(self, request):
        return HttpResponse("响应get请求")
    def post(self, request):
        return HttpResponse("响应post请求")
路由层:
url('^path/$', views.MyCBV.as_views())
'''

文件上传

'''
前端:
<form action="/path/" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <input type="file" name="files" multiple>
    <input type="submit" value="上传">
</form>

后端:
files = request.FILES.getlist('files', None)
for file in files:
    with open(file.name, 'wb') as f:
        for line in file:
            f.write(line)
'''

猜你喜欢

转载自blog.csdn.net/qq_38746842/article/details/88747207
今日推荐