阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Django上传文件代码

110次阅读
没有评论

共计 1553 个字符,预计需要花费 4 分钟才能阅读完成。

在 django 里面上传文件
views.py
# Create your views here.
# coding=utf-8
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.csrf import csrf_protect
# 上传文件
@csrf_exempt
@csrf_protect
def upload_tomcat_config_file(request):
    from django import forms
    class UploadFileForm(forms.Form):
        title = forms.CharField(max_length=1000000)
        file = forms.FileField()
    if request.method == “GET”:
        data=’get’
    if request.method == “POST”:
        f = handle_uploaded_file(request.FILES[‘t_file’])
    return render_to_response(‘upload_config_file.html’,context_instance=RequestContext(request))
    #return HttpResponse(data)
def handle_uploaded_file(f):
    f_path=’/srv/salt/config/’+f.name
    with open(f_path, ‘wb+’) as info:
        print f.name
        for chunk in f.chunks():
            info.write(chunk)
    return f
# 上传文件结束

upload_config_file.html 内容如下

<!DOCTYPE HTML>
<html lang=”en”>
<head>
    <meta charset=”utf-8″ />
    <title>{{title}}</title>
</head>
<body>
    <a> 配置文件上传 </a>
    <form action=”/opsapi/command/up_file” method=”post” enctype=”multipart/form-data” accept-charset=”utf-8″>
        {% csrf_token %}
        <input type=”file” name=”t_file” value=”” />
        <button>Submit</button>
    </form>
</body>
</html>

url 的配置就不写了,效果如下

Django 上传文件代码

Django 上传文件代码

上传到服务器上面

Django 上传文件代码

我这里是用时间来保存的,代码和上面稍有不同

Django 的详细介绍 :请点这里
Django 的下载地址 :请点这里

推荐阅读:

Ubuntu Server 12.04 安装 Nginx+uWSGI+Django 环境 http://www.linuxidc.com/Linux/2012-05/60639.htm

Django 实战教程 http://www.linuxidc.com/Linux/2013-09/90277.htm

Django Python MySQL Linux 开发环境搭建 http://www.linuxidc.com/Linux/2013-09/90638.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计1553字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中