【问题标题】:Unable to upload Images in Python无法在 Python 中上传图像
【发布时间】:2013-09-20 12:50:35
【问题描述】:

模型

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',
)

【问题讨论】:

  • 尝试时会发生什么?
  • 你需要设置STATIC settings
  • @FoxMaSk 请检查编辑。

标签: python django django-admin


【解决方案1】:

该错误并不表示您的上传失败;据说上传后找不到文件。因此,就像文档中关于“Serving uploaded content”的解释一样,您还需要按照他们向我们建议的方式设置媒体设置。

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你的文件也

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 2020-12-13
    • 2017-04-17
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多