Python Django:文件上载与文件创建(上载不工作)
我有两种形式:一种是创建一个新对象(然后为其创建一个新的空文件),另一种是使用现有文件创建一个对象(上传文件)。 创建表单工作正常,它使用新创建的对象的名称创建一个同名文件,并将其上传到静态文件夹中。然而,第二种形式,它告诉你它得到了文件对象和所有东西,但是文件在任何地方都找不到 我试图更改目录、修改代码和所有内容,但似乎根本不起作用,我不知道问题出在哪里,以下是我的代码: views.py:Python Django:文件上载与文件创建(上载不工作),python,django,Python,Django,我有两种形式:一种是创建一个新对象(然后为其创建一个新的空文件),另一种是使用现有文件创建一个对象(上传文件)。 创建表单工作正常,它使用新创建的对象的名称创建一个同名文件,并将其上传到静态文件夹中。然而,第二种形式,它告诉你它得到了文件对象和所有东西,但是文件在任何地方都找不到 我试图更改目录、修改代码和所有内容,但似乎根本不起作用,我不知道问题出在哪里,以下是我的代码: views.py: def create(request): print request.POST fil
def create(request):
print request.POST
filename = request.POST['name']
f = File(open(filename, "w+"))
structure = Structure(name=request.POST['name'], file=f)
structure.save()
return redirect('/structures')
def upload(request):
print request.POST
structure = Structure(name=request.POST['name'], file=request.POST['file'])
structure.save()
return redirect('/structures')
models.py:
class Structure(models.Model):
name = models.CharField(max_length=120)
file = models.FileField(upload_to='structures')
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
def __str__(self):
return self.name
html模板:
[...]
<!--CREATE-->
<div class="col-md-12">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Créer une nouvelle structure</h3>
</div>
<div class="panel-body">
<form class="form-horizontal" action="/create", method="post">
{% csrf_token %}
<fieldset>
<div class="form-group">
<label for="name" class="col-lg-6 control-label">Nom de la structure</label>
<div class="col-lg-6">
<input type="text" name="name" id="name">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2" align="center">
<button type="submit" value="Create" class="btn btn-primary">Créer</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
<!--END-CREATE-->
<!--UPLOAD-->
<div class="col-md-12">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Mettre en ligne une nouvelle structure</h3>
</div>
<div class="panel-body">
<form class="form-horizontal" action="/upload", method="post">
{% csrf_token %}
<fieldset>
<div class="form-group">
<label for="name" class="col-lg-6 control-label">Structure</label>
<div class="col-lg-6">
<input type="text" name="name" id="name">
<label for="structures"></label>
</div>
</div>
<div class="form-group">
<label for="file" class="col-lg-6 control-label">Fichier de la structure</label>
<div class="col-lg-6">
<input type="file" name="file" id="file">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2" align="center">
<button type="submit" value="Create" class="btn btn-primary">Créer</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
<!--END-UPLOAD-->
[...]
settings.py:
STATIC_URL = '/static/'
MEDIA_ROOT = 'files/'
以下是上载文件时的服务器日志:
我感谢你们的帮助
<QueryDict: {u'csrfmiddlewaretoken': [u'D8Cz7fUkxbpl2hYb3lmjnzm85jzlMjti'], u'name': [u'doctor list'], u'file': [u'doctors1.ods']}>
[01/Nov/2017 10:10:13]"POST /upload HTTP/1.1" 302 0
[01/Nov/2017 10:10:13]"GET /structures HTTP/1.1" 301 0
[01/Nov/2017 10:10:13]"GET /structures/ HTTP/1.1" 200 8195
[01/Nov/2017 10:10:13]“发布/上传HTTP/1.1”302 0
[01/Nov/2017 10:10:13]“GET/structures HTTP/1.1”301 0
[01/Nov/2017 10:10:13]“GET/structures/HTTP/1.1”200 8195
顺便说一句:在html模板中,我显示了每个对象的文件路径,当我创建对象并因此创建一个新文件时,路径如下所示:
structures/file.ext
,但当我上传它时,它仅显示如下:files.ext
您应该更改的内容很少:
1-使用表单处理上传:
在您的表单.py
中:
class FileUpload(forms.ModelForm):
class Meta:
model = Structure
fields = ('file', 'name')
form = FileUpload(request.POST, request.FILES)
if form.is_valid():
form.save()
# you can also do some other stuff before saving the form.
在您的视图.py
中:
class FileUpload(forms.ModelForm):
class Meta:
model = Structure
fields = ('file', 'name')
form = FileUpload(request.POST, request.FILES)
if form.is_valid():
form.save()
# you can also do some other stuff before saving the form.
2-将enctype
添加到您的表单中:
更改此项:
<form class="form-horizontal" action="/upload", method="post">
我没有使用forms.py。也许是时候开始使用它们了。我试试这个,然后再打给你。谢谢@我看不出有什么理由不这么做。如果您尝试一下,您会发现它非常有用。它给了我一个错误:没有定义名称“Files”。位于my forms.py文件第5行的..@KaissB,它是您应该将其更改为您的模型名称。我编辑了代码。现在应该可以工作了。所以我也应该从。模型导入结构,对吗?
file = form.save()
# You can get the file url like this:
print(file.file.url)
# Or file path:
print(file.file.path)