So i've been googling this issue for the past hour and can't come up with a solution. Basically this is it: in my model.py i have a class that has this

class Case(models.Model):
zoomOutImage = models.ImageField('Label', upload_to="zoomOutImage")

and in my settings.py i have my media URL/ROOT set up like this

MEDIA_ROOT = os.path.join(os.path.abspath(''),'app/static/ds/')
MEDIA_URL = '/static/ds/'

which from the webserver should serve out like this:

http://127.0.0.1:8000/static/ds/zoomOutImage/actinic_granuloma_3.jpg

I've installed PIL (inside virtualENV) and there are no errors in uploading, the only issue is when i try uploading the file via the admin panel nothing happens. No errors nothing. The file just simply doesn't get uploaded to the zoomOutImage folder by the development server. Can anyone point me towards why?

有帮助吗?

解决方案

I guess your file is in a subdir of your root, subdir named 'zoomOutImage'. Or even a file called like that in the root. I remember putting a function call in the upload to string. That function creates a path and filename, using os.join and the filename from the instance. Doing this by head, no example code available right now. But must be able to google this.

Look here https://stackoverflow.com/questions/1190697/django-filefield-with-upload-to-determined-at-runtime

And by the way, I totally disagree with your answer, you should NEVER use absolute paths in your settings! See this answer use css in django 1.4 development for how to use the correct settings and refer to your Project PATH

EDIT (after reading your own answer)

Guess you are missing this first step:

this is the path to your settings.py file:

SETTINGS_DIR = os.path.dirname(os.path.realpath(__file__))

and than this is the path to your project dir: (I Am using buildout, so call it buildout, but it's the root of your project):

BUILDOUT_DIR = os.path.abspath(os.path.join(SETTINGS_DIR, '..'))

and from there on you can define everything you want:

STATIC_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'static')
STATIC_URL = '/static_media/'
MEDIA_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'media')
MEDIA_URL = '/media/'

and in your template file refer to the image like:

<a href="{{MEDIA_URL}}{{ case.zoomOutImage }}"><img src="{{MEDIA_URL}}{{ case.zoomOutImage }}" width="100%"></a>

when your object given to the template is called case

about your question of the urls. you should add this:

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'', include('staticfiles.urls')),
    )

and see the link above to the question about using css, it's the same problem, but there for finding the css files during development. It's all about the static file places.

其他提示

import os
# get abspath
def rel(*x):
   return os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
MEDIA_ROOT = rel('media')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = '' #if only your static files are in project folder
STATICFILES_DIRS = ( rel('static'),) #if only your static files are in project folder

use this settings, and everything will work

so i finally solved my problem. For anyone having this issue in the future do the following:

if you're trying to serve static media files locally on the development server use absolute paths for MEDIA_ROOT and MEDIA_URL.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top