[Django] #27052: Malformed url of uploaded files; uploaded_to parameter is ignored

6 views
Skip to first unread message

Django

unread,
Aug 11, 2016, 6:20:55 PM8/11/16
to django-...@googlegroups.com
#27052: Malformed url of uploaded files; uploaded_to parameter is ignored
--------------------------------------+--------------------
Reporter: Laisvas | Owner: nobody
Type: Bug | Status: new
Component: File uploads/storage | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
1)If your model has a FileField - field with upload_to parameter, the
parameter is ignored.
2)File.url method on files attached to objects returns MEDIA_URL + full
absolute file path, not the url of the uploaded file.


== How to reproduce: ==

1. Fresh Django install, the project name: mysite, the app name: app
2.

{{{
#models.py:
from django.db import models

class InputFile(models.Model):
name = models.CharField(max_length=255)
upload = models.FileField(upload_to='uploads/%Y/%m/%d/' )

#views.py:

from django.shortcuts import render
from django.http import HttpResponse
from models import InputFile
from django.core.files import File

def index(request):
tmp_file=open('/home/test/mysite/app/media_files/a.txt')
django_file = File(tmp_file)
file_name = django_file.name
file_size = django_file.size
saved = InputFile(name='a', upload=django_file)
saved.save()
## Let's check does the file is saved:
file_pk = saved.pk
file_url = saved.upload.url
upload_path = saved.upload.path
output = 'File name: {0}, file size: {1}, file_pk: {2}, url: {3},
path: {4} '.format(file_name, file_size, file_pk, file_url, upload_path)
return HttpResponse(output)

# settings.py:

MEDIA_URL = '/media/'
MEDIA_ROOT = "/home/test/mysite/app/media_files/"

# urls.py
from django.conf.urls import url
from django.contrib import admin
from app import views

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^file/', views.index),

]

}}}

3. Put a test file in your MEDIA_ROOT (i.e.
'/home/test/app/media_files/a.txt'
4. The output:
File name: /home/test/mysite/app/media_files/a.txt,
file size: 3,
file_pk: 1,
'''url: /media/home/test/mysite/ap/media_files/a_55k5fIW.txt,'''
path: /home/test/mysite/file/media_files/a_55k5fIW.txt
5. The file was saved to /home/test/mysite/file/media_files/a_55k5fIW.txt

== Expected result ==
Django v. 1.9.8 returns on the same code:
...
url: /media/uploads/2016/08/11/a.txt,
path: /home/test/mysite/app/media_files/uploads/2016/08/11/a.txt
...
The file was saved to
/home/test/mysite/app/media_files/uploads/2016/08/11/a.txt

== NB ==
The same problem is on Django version 1.11.dev20160811204658

--
Ticket URL: <https://code.djangoproject.com/ticket/27052>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 11, 2016, 7:21:29 PM8/11/16
to django-...@googlegroups.com
#27052: Malformed url of uploaded files; uploaded_to parameter is ignored
-------------------------------------+-------------------------------------
Reporter: Laisvas | Owner: nobody
Type: Bug | Status: closed
Component: File | Version: 1.10
uploads/storage |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* needs_docs: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_better_patch: => 0


Comment:

You need to use `FieldFile` when interacting with `FileField` if you want
`upload_to` to be respected. Please take a look at the
[https://docs.djangoproject.com/en/dev/topics/files/ file topic guide] for
an example. If you need help, take a look at
TicketClosingReasons/UseSupportChannels for ways to get help. Thanks!

--
Ticket URL: <https://code.djangoproject.com/ticket/27052#comment:1>

Reply all
Reply to author
Forward
0 new messages