#10410 closed Uncategorized (worksforme)
FileField saves one filename on disk and another on DB
| Reported by: | Elland | Owned by: | Peter Bengtsson |
|---|---|---|---|
| Component: | File uploads/storage | Version: | |
| Severity: | Normal | Keywords: | FileField valid filename |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When saving a file with spaces on the name, it will be saved with underscores for spaces on disk, but the database will point to it without underscores,
but with the regular spaces, leading to a File Not Found error.
Change History (11)
comment:1 by , 17 years ago
| milestone: | 1.2 → 1.1 |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 17 years ago
| Component: | Uncategorized → File uploads/storage |
|---|
comment:3 by , 17 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:4 by , 17 years ago
comment:5 by , 17 years ago
| Resolution: | → invalid |
|---|---|
| Status: | assigned → closed |
This test proves that the bug is not reproducable
models.py
from django.db import models
class MyModel(models.Model):
my_file = models.FileField(upload_to='.')
tests.py
import os
from django.test import TestCase
from django.conf import settings
from models import MyModel
class SimpleTest(TestCase):
def test_file_upload(self):
# create a fake file to upload
instance = MyModel.objects.create()
try:
open('upload me.txt','w').write('any junk\n')
from django.core.files import File
f = File(open('upload me.txt'))
settings.MEDIA_URL = ''
instance.my_file.save('with spaces.txt', f)
# django replaces all the spaces with underscores
assert os.path.isfile('with_spaces.txt')
# but in the database it will be 'with spaces.txt'
self.assertEqual(instance.my_file, u'./with_spaces.txt')
self.assertEqual(instance.my_file.url, './with_spaces.txt')
finally:
os.remove('upload me.txt')
if os.path.isfile('with_spaces.txt'):
os.remove('with_spaces.txt')
I think what this proves is that there's no problem with "with spaces.txt" once the file has been uploaded.
comment:6 by , 16 years ago
| Resolution: | invalid |
|---|---|
| Status: | closed → reopened |
I can reproduce this bug. It also exists if there is a file with the same name. If you upload two images with the same name, then when you try to show the first image, the second image is shown. (because the second image get a '_' appended to it, but the name in the database does not)
I wrote a bug report here:
http://code.djangoproject.com/ticket/11504
I originally thought it was specific to django-storages, so I submitted a bug report to django-storages:
http://code.welldev.org/django-storages/issue/13/the-database-doesnt-store-the-correct-file-name
comment:7 by , 16 years ago
| Resolution: | → worksforme |
|---|---|
| Status: | reopened → closed |
peterbe has contributed an executable test case that implements the problem case described in the original ticket, and that test case passes on Django Trunk under SQLite, Postgres and MySQL.
I don't doubt that you are experiencing a problem, but at this point, the onus is on you to establish how your error case differs from the working test case that we have. We can't fix any bug that we can't reproduce.
comment:8 by , 16 years ago
Where in the code-base does Django write to the database? I want to see if I can trace is myself, to see what I have done wrong.
comment:10 by , 11 years ago
| Easy pickings: | unset |
|---|---|
| Severity: | → Normal |
| Type: | → Uncategorized |
| UI/UX: | unset |
I also ran into this issue, and traced the issue to:
https://github.com/django/django/blob/master/django/utils/text.py#L234
and
https://github.com/django/django/blob/master/django/core/files/storage.py#L65
Unable to reproduce. Currently on revision 10189.
When uploading a file called "my file.txt" it gets saved on the filesystem as "my_file.txt" and in SQL it's stored as "./my_file.txt".
Thus, there's no reference back to "my file.txt".
Also I don't know how to actually reproduce the "leading to a File Not Found error". What does that mean? How are you *viewing* the FileField