【问题标题】:Cannot get images to display in simple django site无法在简单的 django 站点中显示图像
【发布时间】:2010-01-27 16:54:19
【问题描述】:

我有一个非常简单的 django 网站,但我无法在管理面板中显示上传的图片。

我的settings.py 有这些常量:

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = '/home/jeroen/programming/python/django/sitename/media'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = 'http://127.0.0.1:8000/media/'

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'

我的urls.py 看起来像这样:

from django.conf.urls.defaults import *
from django.conf import settings

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^sitename/', include('sitename.foo.urls')),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
    # to INSTALLED_APPS to enable admin documentation:
    (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),

    # http://docs.djangoproject.com/en/dev/howto/static-files/
    # This method is inefficient and insecure. 
    # Do not use this in a production setting. 
    # Use this only for development.
    (r'^media/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.MEDIA_ROOT}),
)

而我的models.py 看起来像这样:

from django.db import models

class Truitje(models.Model):
    titel = models.CharField(max_length=50)
    beschrijving = models.TextField(max_length=500)
    foto = models.ImageField(upload_to='truitjes')

    def __unicode__(self):
      return self.titel

我可以在管理界面成功上传图片,并存储在/home/jeroen/programming/python/django/sitename/media/truitjes。但是当我转到例如http://127.0.0.1:8000/media/truitjes/DSC00068.JPG 时,我得到一个错误:Page not found: /media/truitjes/DSC00068.JPGhttp://127.0.0.1:8000/media/truitjes 和 ``http://127.0.0.1:8000/media/givesPermission denied: /media/` 也是如此。

【问题讨论】:

    标签: django image


    【解决方案1】:

    将您的媒体或管理 URL/前缀更改为不同的内容。

    它们不能具有相同的值。如果他们这样做,ADMIN_MEDIA_PREFIX 具有优先权。这意味着如果您尝试访问 http://127.0.0.1:8000/media,您正在尝试访问管理媒体文件夹。

    要么更改ADMIN_MEDIA_PREFIX

    ADMIN_MEDIA_PREFIX = '/admin-media/'
    

    或更改MEDIA_ROOT

    # in settings.py
    
    MEDIA_ROOT = '/home/jeroen/programming/python/django/ninikske/static'
    MEDIA_URL = 'http://127.0.0.1:8000/static/'
    
    # and in url.conf
    
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',
     {'document_root': settings.MEDIA_ROOT}),
    

    【讨论】:

      【解决方案2】:

      在 Django 1.3 中,如果您只是想在开发过程中在视图中显示图像,请按照以下步骤操作:

      1. 在您的应用目录中创建一个名为“static”的文件夹
      2. 将图片放在静态文件夹中
      3. 确保 settings.py 中的 STATIC_URL 设置为 '/static/' - 这应该是默认值
      4. 在你的 app/urls.py 中:

        from django.contrib.staticfiles.urls import staticfiles_urlpatterns
        #..rest of url.py config...
        urlpatterns += staticfiles_urlpatterns()
        
      5. 在您的模板中,无论您希望图片显示在何处,都可以使用此模板标签:

        <img src="{{ STATIC_URL }}pic.png" />
        

      在此处查看 Serving Static Files in Development:https://docs.djangoproject.com/en/1.3/howto/static-files/#staticfiles-in-templates

      【讨论】:

      • 我想补充一下这个非常好的答案,如果你有全局的静态文件并且在我仔细阅读文档后发现的一般“静态”目录中,你可以添加目录使用以下代码进入 STATICFILES_DIRS 元组: os.path.join(os.path.realpath(os.path.dirname(file)),'static'), - 这对 CSS 很有用,例如。
      猜你喜欢
      • 2022-10-13
      • 1970-01-01
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      • 2020-03-19
      相关资源
      最近更新 更多