无法在Python中上传图像

时间:2013-09-16 11:30:13

标签: python django django-admin

模型

image_url = models.ImageField(upload_to="uploads/shows",blank=True,null=True)

我需要将图像上传到特定目录。我正在尝试使用管理面板添加这些媒体文件。如何上传图片?

修改1

http://192.168.2.9:8000/static/uploads/shows/myImage.jpg

Page not found (404)
Request Method: GET
Request URL:    http://192.168.2.9:8000/static/uploads/shows/myImage.jpg
'uploads/shows/myImage.jpg' could not be found
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

修改2

    # Absolute path to the directory static files should be collected to.
    # Don't put anything in this directory yourself; store your static files
    # in apps' "static/" subdirectories and in STATICFILES_DIRS.
    # Example: "/home/media/media.lawrence.com/static/"
    STATIC_ROOT = ''

    # URL prefix for static files.
    # Example: "http://media.lawrence.com/static/"
    STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

1 个答案:

答案 0 :(得分:0)

错误并未表示您的上传失败;它说它一旦上传就找不到文件。就像文档解释“Serving uploaded content”一样,您还需要像我们建议的那样设置MEDIA设置。

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

然后you will be able to upload您的文件