2017-06-22 124 views
0

我是个新手,上传文件,在它的代码,以便即时通讯相当糟糕。我使用Django作为框架。目前这是我的用户输入html代码。如何重新命名输入文本

<html> 
<head> 
    <title>uploader</title> 
    <style> 
    h1 { 
     font-size: 2em; 
     font-weight: bold; 
     color: #777777; 
     text-align: center 
    } 
    table { 
     margin: auto; 
    } 
    a { 
     text-decoration: none 
} 
    </style> 
</head> 

<body> 

    <h1> 
     {{what}} 
     <h3><p style="text-align: left;text-decoration: none;"><a href="{% url 'home' %}">Back</p></a></h3> 
    </h1> 
<hr> 
    <table> 
    <tr> 
     <td> 
     <form action="upload/" method="POST" enctype="multipart/form-data"> 
{% csrf_token %} 
<fieldset> 
    Category of the device:<br> 
    <input type="text" name="category" value="" required id="cat"> 
    <br> 
    Model:<br> 
    <input type="text" name="model" value="" required id="mod"> 
<br> 
      <input type="submit" value="Upload File" class="submit" name="submit" /> 
<input type="file" name="file"/> 

</fieldset> 

     </form> 


     </td> 
    </tr> 
    </table> 

</body> 

</html> 

views.py

def home(request): 
    return render(request, 'index.html', {'what':'Upload Files'}) 


def upload(request): 
try: 
    if request.method == 'POST' and request.FILES['file']: 
     try:  
      myfile = request.FILES['file'] 
      category = request.POST['category'] 
      model = request.POST['model'] 
      validate_file_extension(myfile) 
      fs = FileSystemStorage(location='uploaded/') 
      filename = request.FILES['filename'].name 
      handle_uploaded_file(request.FILES['file'], str(request.FILES['file'])) 
      modified_name = "{}_{}_{}".format(filename, category, modle) 
      filename = fs.save(modified_name, myfile) 
      return redirect("/success") 
     except: 
      return redirect("/unsuccessful") 
except: 
    return redirect("/upload") 

def handle_uploaded_file(file, filename): 
    if not os.path.exists('uploaded/'): 
     os.mkdir('uploaded/') 
    with open('uploaded/' + filename, 'wb+') as destination: 
     for chunk in file.chunks(): 
      destination.write(chunk) 

我如何重命名上传的用户具有的文字,他们输入自己的文件名后面用“_”的文件。

如:

filename = myfile 
category = camera 
model = XYZ123 

该文件应该被重新命名为 “myfile_camera_XYZ123”。

+0

你必须做,在视图中。你可以发布吗? –

+0

当然!我编辑过它 –

+0

与模型相关联的形式? –

回答

0

首先获取表单值,categorymodel。然后从上传的文件中获取文件名。

category = request.POST['category'] 
model = request.POST['model'] 

# And filename 
filename = request.FILES['filename'].name 


modified_name = "{}_{}_{}".format(filename, category, modle) 
filename = fs.save(modified_name, myfile) 
+0

我没有得到你想说的,我试图添加代码,但它不工作。 –

+0

从窗体获得cateogry和模型后。并使用request.FILES ['filename']获取原始文件名。name –

+0

然后创建修改后的名称字符串。你不明白这个...? –

0

这里有一个更好的方法来处理Django窗体。

# forms.py 
from django import forms 

class UploadFileForm(forms.Form): 
    category = forms.CharField(max_length=100) 
    model = forms.CharField(max_length=100) 
    file = forms.FileField() 

# views.py 
def upload(request): 
    if request.method == 'POST': 
     form = UploadFileForm(request.POST, request.FILES) 
     if form.is_valid(): 
      data = form.cleaned_data 
      validate_file_extension(data.file) 
      file_ext = data.file.split(".")[-1] 
      file_core = data.file.replace('.'+file_ext, '') 
      filename = '{0}_{1}_{2}.{3}'.format(file_core, data.category, data.model, file_ext) 
      handle_uploaded_file(filename) 
      # ... fill the end 
+0

即使世界一个字典的错误,我不能在“file_ext = data.file.split(”。“) - 1]” 和当u说填补了年底,我该怎么办与节选?如果我改变它其他显示语法错误 –

+0

forms.py固定,我的坏。应该会更好。根据您的项目不确定代码是100%有效,但它给你的主要想法。 –

+0

对不起,延迟回复,但在cmd它错误显示:AttributeError:'字典'对象没有属性'文件' 这是否意味着什么? –