GET,POST请求以及类视图,文件上传

GET 通过url地址携带参数访问服务器,不太安全
POST 直接发送服务器,能修改服务器,相对安全
views.py 中

from django.shortcuts import render
from django.http import HttpResponse


def index(request):

    print(request.path)#请求路径
    print(request.method)#请求方式
    print(request.encoding)#编码格式

    return HttpResponse('666')




def test(request):

    if request.method == 'GET':

        return render(request, 'get_post/get_post_test.html')

    elif request.method == 'POST':

        # title1 = request.POST.get('title1')
        # title2 = request.POST.get('title2')
        # 如果提交过来的值的name是一样的多个,就用getlist获取
        title1 = request.POST.getlist('title1')

        print(title1)

        return HttpResponse('成功')
        
        
        

html 中

<body>


{#表单#}
<form action="" method="post">

    {#防止跨域请求#}
    {% csrf_token %}

    这两个name是一样的,所以在views.py中,获取时 用getlist获取
    <input type="text" name="title1" id="t1">
    <input type="text" name="title1" id="t2">

{#    单选框#}
    <input type="radio">
{#    复选框#}
    <input type="checkbox">
    
    <input type="submit" value="提交" id="t3">
    
    
</form>




{#<script>#}
{##}
{##}
{#    var t1 = document.getElementById('t1')#}
{#    var t2 = document.getElementById('t2')#}
{#    var t3 = document.getElementById('t3')#}
{##}
{#    t3.onclick = function () {#}
{##}
{#        alert(t1.value + ',' + t2.value)#}
{##}
{#    }#}
{##}
{##}
{##}
{#</script>#}

<img src="{% static 'media/NDA3MzIzNw%253D%253D%252FbmltYV82ODg5NjY5NjUzXzE1NTg5NjQwODg1NzRfMjMyZmY3MjgtNmE4NS00ZTFlLWJiMGMtNmIzYmE5NWI3NjBi.jpeg' %}" alt="">

</body>
类视图 类视图可以自己判断请求是GET 还是 POST 简化了代码
views.py中
    
from django.shortcuts import render, redirect, reverse
from django.http import HttpResponse
from .models import blogModel
导入View
from django.views import View

让类继承自View
class Blog_add(View):

    没带参数
    def get(self, request):

        return render(request, 'blog/demo_add.html')


    def post(self, request):

        title = request.POST.get('title')
        content = request.POST.get('content')

        if title == '' or content == '':

            return HttpResponse('请添加正确的数据!')


        bm = blogModel.objects.create(title=title, content=content)
        bm.save()
      

        # 重新渲染界面
        return render(request, 'blog/demo_add.html')
    




class Blog_update(View):

    带来参数 blogID
    def get(self, request, blogID):

        blog = blogModel.objects.get(id=blogID)
        return render(request, 'blog/demo_update.html', context={


            'blog':blog


        })


    带来参数 blogID
    def post(self, request, blogID):

        if request.POST.get('title') == '' or request.POST.get('content') == '':

            return HttpResponse('数据为空!')

        else:

            blog = blogModel.objects.get(id=blogID)
            blog.title = request.POST.get('title')
            blog.content = request.POST.get('content')
            blog.save()

            return redirect(reverse('blog_list'))
            
            
            
            
            
urls.py中调用views.py中的视图函数

from django.urls import path
from . import views

urlpatterns = [

    # 类视图调用
    path('cls_add/', views.Blog_add.as_view(), name='cls_add'),
    path('cls_update/<blogID>', views.Blog_update.as_view(), name="cls_update"),

]
文件上传
首先在项目中新建一个static文件夹,在文件夹下新建一个子文件夹这里取名media用于存放上传的文件


在主目录的setting.py中 进行配置

STATIC_URL = '/static/'

STATICFILES_DIRS=[

    os.path.join(BASE_DIR, 'static')

]


MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')



新建一个html文件

html文件中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件操作</title>
</head>
<body>


<!--文件上传表单中必须加 enctype="multipart/form-data" 否则会报错-->
<form action="" method="post" enctype="multipart/form-data">

    <!--防止跨域请求-->
    {% csrf_token %}

    <input type="file" name="fileLP">

    <input type="submit">


</form>


</body>
</html>





views.py中

导入os库
import os
导入刚刚配置的文件夹路径
from mysite.settings import MEDIA_ROOT

class UPload(View):

    def get(self, request):

        return render(request, 'blog/files.html')

    def post(self, request):

        file = request.FILES.get('fileLP')#获取到文件
        f_name = os.path.join(MEDIA_ROOT, file.name)#创建文件路径

        with open(f_name, 'wb') as f:#用上下文管理器打开文件路径

            for i in file.chunks():
                f.write(i)


        return HttpResponse('写入完成')
        
        
        
        

urls.py中的路由

from django.urls import path
from . import views

urlpatterns = [

    path('clsUpload/', views.UPload.as_view(), name='upload'),

]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值