zoukankan      html  css  js  c++  java
  • python

    #
    """
    
    一:# 视图 (接收请求返回响应的那部分)
    FBV版:基于 函数的 请求
    
    CBV版:基于 类的 请求
    
    
    注册方法:
        url(r'^add_publisher/', views.Addpublisher.as_view()),
    
        # CBV版
    from django.views import View   # 引入View
    class Addpublisher(View):       # 创建类
        def get(self,request):      # get 方法
            return render(request,'add_publisher.html',)
    
        def post(self,request):     # post 方法  
            new_name = request.POST.get('publisher_name')
            if new_name:
                models.Publisher.objects.create(name=new_name)
                return redirect('/publisher_list/')
            else:
                return render(request,"add_publisher.html")
        
        
    二:# request 对象
        request.method       -->  获取请求方式( GET,POST .. )
        request.GET.get()    -->  来获取 URL 里面的参数
        request.POST.get()   -->  来获取 URL 里面的参数
        
        request.path_info    -->  获取用户请求的路径( 不包含Ip,端口,URL参数 )
        request.body         -->  
        
    三:# response
        HttpResponse    -->  返回字符串内容
        Render          -->  返回一个HTML页面
        Redirect        -->  返回一个重定向(告诉浏览器再去访问另外的网址)
        JsonResponse    -->  将数据返回页面
            from django.http import JsonResponse    # Django 封装的 JsonResponse 只能识别字典形式的数据
            return JsonResponse(data1,safe=False)   # 单独返回 列表类型的数据 可以使用 safe=False 
    
    """#
  • 相关阅读:
    自动化测试常用断言的使用方法
    python接口自动化-有token的接口项目使用unittest框架设计
    postman
    HTML5基础
    HTML基础
    Web常见产品问题及预防
    JSON语法详解
    HTTP协议详解
    接口理论详解
    设计模式之装饰者模式
  • 原文地址:https://www.cnblogs.com/xinzaiyuan/p/12821604.html
Copyright © 2011-2022 走看看