xone

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  114 随笔 :: 0 文章 :: 2 评论 :: 58959 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

 request可以直接在前端用,不需要在render里再定义。

 

 

request.body  //前端页面数据内容的原生值。如果前端不是从POST,GET,FILES里面获取的数据,就从body里获取

  request.POST  //从request.body里拿数据再封装到request.POST里面

  request.FILES  //从request.body里拿数据再封装到request.FILES里面

  request.GET  //从request.body里拿数据再封装到request.GET里面

  request.XXX.getlist  //获取列表

        request.post.getlist  //获取列表

 

request.Meta  (request headers)  //获取请求头相关的信息。

  request.method  //请求方法

  request.path_info  //获取URL

  request.COOKIES  //获取cookies

 

cookie信息或在rep里添加的键值对将会写到response headers里面

a= 'python'

rep = HttpResponse(...) 或 rep = render(request, ...)或rep =redirect('/index/')

 

a= 'python'

rep = HttpResponse(a)

rep['name'] = 'zhou'

rep.set_cookie()

return rep

 

HttpResponse可以返回字节和字符串

 

复制代码
def index(request):
    from django.core.handlers.wsgi import WSGIRequest
    print(type(request))

    print(request.environ)
    for k,v in request.environ.items():  //查看所有用户请求信息 
print(k,v) print(request.environ['HTTP_USER_AGENT'])  //查看请求终端类型
return HttpResponse('OK')
复制代码

 

用HttpResponse发送图片

urls

    url(r'^image.html', views.get_image),

 

视图函数

def get_image(request):
    with open('static/imgs/16.jpg','rb') as fp:
        data = fp.read()
    return HttpResponse(data)

 

前端html

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<img src="/image.html">

</body>
</html>
复制代码

 

CBV

实例:

视图函数

复制代码
from django.views import View
class Home(View):

    def dispatch(self, request, *args, **kwargs):
        print('before')
        result = super(Home,self).dispatch(request,*args,**kwargs)
        print('after')
        return result

    def get(self,request):  # GET方式提交
        print('get')

        return render(request,'home.html')

    def post(self,request):  # POST方式提交
        print('post')

        return render(request,'home.html')
复制代码

 

urls

urlpatterns = [

    url(r'^home/', views.Home.as_view()),

]

 

posted on   周小百  阅读(9215)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
点击右上角即可分享
微信分享提示