2017-06-16 31 views
0

我想管理(移動和重命名)管理的上傳文件用戶上傳:如何在Django

我上傳表單(HTML):

<form action="../valid_upload/" method="POST" enctype="multipart/form-data"> 
    {% csrf_token %} 
    {{form.as_p}} 
    <input type="submit" name="send" value="send" /> 
</form> 

我的表格(Django的部分):

class UploadImageForm(forms.Form): 
image = forms.FileField() 
name = forms.CharField(max_length=100) 
about = forms.CharField(widget=forms.Textarea) 
taq1 = forms.CharField(max_length=100) 
taq2 = forms.CharField(max_length=100) 
taq3 = forms.CharField(max_length=100) 

url.py(只是一個一行):

和view.py(只是其中的一部分):

if 'username' in request.session: 
    if request.method == 'POST': 
     if 'image' in request.FILES: 
      form = UploadImageForm(request.POST, request.FILES) 
      if form.is_valid(): 
       # Here goes the documentation code 
      return HttpResponse(request.FILES['image'].content_type) 
      //here i want to rename and move uploaded files 
     else: 
      return redirect('/upload_image') 
    else: 
     return redirect('/login/') 

我想知道如何重命名上傳的文件和移動他們對我的directories.if你能幫助我:)

回答

0

你可以將請求數據傳遞給您的表單,並讓表單管理您的需求。這裏是一個例子:

if 'username' in request.session: 
    if request.method == 'POST': 
     if 'image' in request.FILES: 
      form = UploadImageForm(request.POST, request.FILES) 
      if form.is_valid(): 
       # Here goes the documentation code 
      return HttpResponse(request.FILES['image'].content_type) 
      //here i want to rename and move uploaded files 
     else: 
      return redirect('/upload_image') 
    else: 
     return redirect('/login/') 

你也可以使用django模型。 這裏是文檔: https://docs.djangoproject.com/en/1.11/topics/http/file-uploads/

+0

什麼是UploadFileForm? – snip1377

+0

你只需複製一部分代碼,然後過去! – snip1377

+0

我向您展示了文檔示例。這就是爲什麼我粘貼鏈接。 –