django的文件上传&JsonResponse的使用&数据库的连接

  1 request对象

                   method:请求方式

        GET:get请求的参数(post请求,也可以携带参数)

        POST:post请求的参数(本质是从bdoy中取出来,放到里面了)

        COOKIES:

        META:字典(放着好多东西,前端传过来的,一定能从其中拿出来)

        body:post提交的数据

        path:请求的路径,不带参数

        request.get_full_path() 请求路径,带参数

        session:

        user:

        FILES

        encoding:编码格式

                  

                   is_ajax():

         2 HttpResponse对象

                   -三件套

                   法一:

                 HttpResponse(json.dumps(dic,ensure_ascii=False))

                  法二:

                   From django.http import JsonResponse

                   -JsonResponse:往前端返回json格式数据(没有它,我可以自己写)

                            -转列表格式:指定safe=False

                            -中文字符问题:json_dumps_params={'ensure_ascii':False}

        

         3 wsgiref,uwsgi,---都遵循wsgi协议

                   Wsgiref自己写代码时用,uwsgi上线用(可以解决高并发)

                   -遵循一个协议wsgi(Web Server Gateway Interface web服务网关接口)

         4 CBV(基于类的视图)和FBV(基于函数的视图)

                   -cbv:一个路由写一个类

                   -先定义一个类:继承自View

                            from django.views import View

                            class MyClass(View):

                                     # 当前端发get请求,会响应到这个函数

                                     def get(self, request):

                                               return render(request,'index.html')

                                     # 当前端发post请求,会响应到这个函数

                                     def post(self,request):

                                               print(request.POST.get('name'))

                                               return HttpResponse('cbv--post')

                   -在路由层:

                            re_path('^myclass/$',views.MyClass.as_view()),

                  

         5 文件上传

                   -form表单默认提交的编码方式是enctype="application/x-www-form-urlencoded"

                   -前端:如果要form表单上传文件,必须指定编码方式为:multipart/form-data

                   -后端:

                            file=request.FILES.get('myfile')

                            with open(file.name,'wb') as f:

                                     for line in file:

                                               f.write(line)

                           

         6 前端提交数据编码格式:

                   -multipart/form-data(上传文件)

                   -application/x-www-form-urlencoded(默认编码)

        

         7 图书管理系统表分析:

                   图书管理系统

                   -表:

                   book表

                   author表

                   publish表

                  

                   -一对一:一对多的关系一旦确立,关联字段写在哪都可以

                   -一对多:一对多的关系一旦确立,关联关系写在多的一方

                   -多对多:多对多的关系,必须创建第三张表(中间表)  

        

         8 models中写的class时 的注意点

                   class Book(models.Model):

             id = models.AutoField(primary_key=True)

             name = models.CharField(max_length=32)

             pub_date = models.DateField()

             # max_digits:最长是5,decimal_places:小数点后两位

             price = models.DecimalField(max_digits=5, decimal_places=2)

             # to='Publish',to_field='id':外键关联到publish表的id字段

    # to 哪个表的引号可以去掉,前提是这个表必须在前面定义,推荐加引号的(使用了ForeignKey,它会自动在publish后面加_id变成publish_id)

             publish = models.ForeignKey(to='Publish', to_field='id',on_delete=models.CASCADE)

             # 写了这一句,相当于创建了第三章表(手动创建第三张表也可以,后面表),不会在数据库创建authors字段

             authors = models.ManyToManyField(to='Author')

         9 settings中的配置文件(表格插入mysql)

    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': '0109',
    'HOST': '127.0.0.1',
    'PORT': '3306',
    'USER': 'root',
    'PASSWORD': 'root',
}

猜你喜欢

转载自www.cnblogs.com/zhouhai007/p/10247267.html