views:

338

answers:

1

I have something like:

MEDIA_ROOT = '/home/httpd/foo/media/'
MEDIA_URL = 'http://www.example.org/media/'

(...)

file = models.FileField(upload_to='test')

When I create an object with that field in the admin page Django stores in the DB the full file path, like: "/home/httpd/foo/media/test/myfile.pdf". This is contrary to what says in the docs.

All that will be stored in your database is a path to the file (relative to MEDIA_ROOT).

When I use the file.url in a template I get something like:

http://www.example.org/home/httpd/foo/media/test/myfile.pdf

instead of what I would like:

http://www.example.org/media/test/myfile.pdf

What am I doing wrong?

+1  A: 

I just did a sample FileField in one of my projects and it seemed to work as you are expecting. Here are a couple things to try.

Try making your settings like the following. I know they say it is bad to not end your MEDIA_URL with a / but this is how I do it and I like it better. You just have to remember whenever you use MEDIA_URL in a template to follow it with a slash: href="{{ MEDIA_URL }}/path/to/file"

MEDIA_ROOT = '/home/httpd/foo/media'
MEDIA_URL = '/media'

If that doesn't help anything, create a new simplified model with a FileField nothing customized and see if you are still getting the same problem.

sheats
You are right, it was working the way you said it. I suspect that what happened was that the web server was still using the old python files. I transitioned to the production server recently so I sometimes still forget to 'touch' the configuration file, to apply the changes.
Luís Marques