Reputation: 25
I have two forms: one where you create a new object (then it creates a new empty file for it), and one where you create an object with an existing file (it uploads the file). The creation form works just fine, it takes the name of the new created object and creates a file with the same name and uploads it in the static folder. However, the second form, it tells you that it got the file object and everything, but the file is not found anywhere.
I tried to change directories and modify code and everything and it doesn't seem to work at all and I do not know where is the issue, here are my codes:
views.py:
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 template:
[...]
<!--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-->
[...]
urls.py:
url(r'^create$', views.create),
url(r'^upload$', views.upload),
settings.py:
STATIC_URL = '/static/'
MEDIA_ROOT = 'files/'
Here is the server log when uploading a file:
I appreciate the help guys.
<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
By the way: in the html template I show the file path of each object, when I create the object and thus create a new file, the path is shown like this structures/file.ext
but when I upload it, it only displays it like this: files.ext
Upvotes: 1
Views: 743
Reputation: 4208
Well there is few things you should change:
1- Using forms to handle uploading:
in your forms.py
:
class FileUpload(forms.ModelForm):
class Meta:
model = Structure
fields = ('file', 'name')
in your views.py
:
form = FileUpload(request.POST, request.FILES)
if form.is_valid():
form.save()
# you can also do some other stuff before saving the form.
2- Adding enctype
to your form:
Change this:
<form class="form-horizontal" action="/upload", method="post">
to :
<form class="form-horizontal" action="/upload", method="post" enctype="multipart/form-data">
This should solve your problem.
And for last part: You're using forms to upload a file. so if your file name already exist in upload folder, form will add some random words and numbers to end of the file. so you can't show the file path like that, it might be wrong. you can make an instance of uploaded file and get the name or path.
it would be something like this:
file = form.save()
# You can get the file url like this:
print(file.file.url)
# Or file path:
print(file.file.path)
Upvotes: 1