zoukankan      html  css  js  c++  java
  • django 获取表单内容 以及文件的上传

    获取 type='text' 标签中的内容 

    使用 request.POST.get( 'name标签的值',None )

    view.py 中的代码

    1 def index(request):
    2      if request.method =='POST':
    3     u = request.POST.get('user',None)     #user 是标签的name属性的值
    4     p = request.POST.get('pwd',None)
    5     print(v,'post')
    6     return render(request,'index.html')
    7 else:
    8      return render(request, 'index.html')

    模板中 index.html的代码

        <form action="index/" method="POST">
            <input type="text" name="user" placeholder="用户名"/>
            <input type="password" name="pwd" placeholder="密码"/>
            <input type="submit" value="提交"/>
        </form>

    2. 获取  input标签中,type=‘radio’ 中的值

    type=radio的时候, name的值 必须相同,所以获取的是 value的值,

    view.py 代码

    1 def index(request):
    2     if request.method =='POST':
    3         v = request.POST.get('genger',None)   #这里get的是 name的属性的值
    4         print(v,'post')
    5         return render(request,'index.html')
    6     else:
    7         return render(request, 'index.html')

    index.html 代码

    1 <body>       
    2         <p>
    3<input type="radio" name="genger" value="1" />
    4<input type="radio" name="genger" value="2" />
    5<input type="radio" name="genger" value="3" />
    6             <input type="submit" value="提交" />
    7         </p>     
    8 </body>                

    获取 select 标签的值 

    1 def index(request):
    2     if request.method =='POST':
    3         v = request.POST.get('city',None)   # get后面写的是 selectname的属性的值
    4 print(v,'post') 5 return render(request,'index.html') 6 else: 7 return render(request, 'index.html')

    index.html 代码

     1 <body>        
     2         <p>
     3         <select name="city">
     4             <option value="44">北京</option>
     5             <option value="55">天津</option>
     6             <option value="66">青岛</option>
     7         </select>
     8         </p>
     9         <p>
    10         <input type="submit" value="提交" />
    11         </p>    
    12 </body>       

    当select 为多选时候 get 换成 getlist

    获取值的时候 get后面写的都是name属性的值, 当获取单个值的时候 用  

    v = request.POST.get('name',None) 
    当获取多个值的时候 用
    v = request.POST.getlist('city',None)   


    文件上传 使用
    request.FILES.get
    文件上传的时候 form标签要做设置 ,标签内加上
    enctype="multipart/form-data
    index.html 代码
     1 <body>
     2     <form action="index/" method="POST" enctype="multipart/form-data">
     3     <p>
     4 
     5     <input type="File" name="fafafa">
     6     <input type="submit" value="提交"    />
     7     </p>
     8 
     9     </form>
    10 </body>
    
    

    views.py 代码

    
    
     1 import os
     2 def index(request):
     3     if request.method =='POST':
     4         obj= request.FILES.get('fafafa')   #get的是 type= ‘file’标签的名字
     5         file_path = os.path.join('upload',obj.name) #upload提前创建的文件夹,用于存放上传的文件  obj.name 是文件名
     6         f = open(file_path , mode = 'wb')
     7         for i in obj.chunks():
     8             f.write(i)
     9         f.close()
    10         return render(request,'index.html')
    11     else:
    12         return render(request, 'index.html')
    
    
    


     
  • 相关阅读:
    Axure 实现数字自动加键功能(点击“+”数字加1,点击“-”数字减1)
    Axure 实现批量的勾选和反选
    传说中的AutoCAD公司
    Autodesk 最新开发技术研讨会-北京-上海-武汉-成都-西安-PPT下载
    发布App,赢iPad mini + 美金100$
    无插件的大模型浏览器Autodesk Viewer开发培训-武汉-2014年8月28日 9:00 – 12:00
    为Autodesk Viewer添加自定义工具条的更好方法
    为Autodesk Viewer添加自定义工具条
    Autodesk 最新开发技术研讨会 -8月22日-Autodesk北京办公室
    请保护我们的地球
  • 原文地址:https://www.cnblogs.com/jiayou888888/p/8067680.html
Copyright © 2011-2022 走看看