如何在django

时间:2017-06-16 14:53:57

标签: python html django django-templates django-views

我想管理(移动和重命名)用户上传的文件:

我的上传表单(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(只有一行):

url(r'valid_upload/', views.valid_upload, name='valid_upload'),

和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/')

我想知道如何重命名已上传的文件并将其移到我的目录上。如果您可以帮助我:)

1 个答案:

答案 0 :(得分:0)

您可以将请求数据传递到表单,然后让表单管理您的需求。这是一个例子:

-u, --uid UID

您也可以使用django模型。 这是文档: https://docs.djangoproject.com/en/1.11/topics/http/file-uploads/