HttpResponse对象


视图在接收请求并处理后,必须返回HttpResponse对象或子对象。HttpRequest对象由Django创建,HttpResponse对象由开发人员创建。

HttpResponse

可以使用django.http.HttpResponse来构造响应对象。

HttpResponse(content=响应体, content_type=响应体数据类型, status=状态码) 也可通过HttpResponse对象属性来设置响应体、响应体数据类型、状态码:

content:表示返回的内容。 status_code:返回的HTTP响应状态码。 响应头可以直接将HttpResponse对象当做字典

Read more

HttpRequest对象


  • 提取URL的特定部分,如/weather/beijing/2018,可以在服务器端的路由中用正则表达式截取
  • 查询字符串(query string),形如key1=value1&key2=value2
  • 请求体(body)中发送的数据,比如表单数据、json、xml
  • 在http报文的头(header)中

url 路径参数

位置参数

url(r'^(\d+)/(\d+)/$', views.index),

def index(request, value1, value2):
      context = {'v1':value1, 'v2&#

Read more

drf嵌套序列化返回


class Books(View):

    def get(self,request):
        # 1、查询所有图书对象
        books = BookInfo.objects.all()

        ser= BookSerialzier(books,many=True)

        return JsonResponse(ser.data, safe=False)

    def post(self, request):
        # 1、获取前端数据
        data = request.body.decode()
        da

Read more

面向对象多态


多态是面向对象编程的三大特性之一,指同一类事物的多种形态。在 Python 中,多态可以通过继承和重写来实现。

具体地说,当一个类定义了一个方法,并且另一个类继承该类并重写了该方法时,我们就拥有了多态性。即无论哪个子类对象调用该方法,都会执行其自身的版本。下面是一个简单的示例代码:

class Animal:
    def sound(self):
        pass

class Dog(Animal):
    def sound(self):
        print("汪汪汪")

class Cat(Animal):
    def sound(self

Read more

python for的用法


sum = 0
for i in range(1,101):
    sum = sum +i
    i += 1
print("1-100之间和是: ",sum)


sum1 = 0
for i in range(1,101):
    if i % 2 == 0:
        sum1 = sum1 +i
    i += 1
print("for--1-100之间偶数的和是: ",sum1)


# 用for循环实现1~100的偶数求和
a=0
for x in range(0,101,2):
    a +=x
print(a)


# 

Read more

join


  select user_basic.user_id,user_basic.mobile,user_relation.target_user_id 
  from user_basic join user_relation on user_basic.user_id=user_relation.user_id 
  where user_basic.mobile='13800001111';

Read more