我可以上传两个不同的文件在同一个文件夹使用django。但是我必须把它上传到两个不同的文件夹,并且把我上传的文件重命名为target.{file\u extension}和probe.{file\u extension}。我不知道,因为我是一个初学者django。可以吗任何人请帮我解决我的问题。 我的代码是:
在django型号.py你知道吗
dirname = datetime.now().strftime('%Y.%m.%d.%H.%M.%S')
class Document(models.Model):
docfile = models.FileField(upload_to=dirname)
在视图.py你知道吗
def test(request):
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile=request.FILES['docfile'])
newdoc.save()
else:
form = DocumentForm() # An empty, unbound form
documents = Document.objects.all()
return render(
request,
'personal/basic.html',
{'documents': documents, 'form': form}
)
在我的心里基本.html你知道吗
<form action="/simulation/" method="post" enctype="multipart/form-data" single>
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
<input type="submit" value="Upload" name = "file1"/></p>
</form>
如果您检查django docs中的FileField,您会看到upload\u to supports custom方法:
(来自django docs)
正如您在自定义方法中看到的,您可以访问当前实例,该实例可用于生成保存文件的自定义路径。你知道吗
相关问题 更多 >
编程相关推荐