无法将图像显示在简单的django站点中 [英] Cannot get images to display in simple django site

查看:137
本文介绍了无法将图像显示在简单的django站点中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的django网站,但是无法获取我在管理面板上显示的图像。

I have a very simple django site but I have trouble getting the images that I upload in the admin panel to show.

我的设置.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 我收到一个错误:页不发现:/media/truitjes/DSC00068.JPG 。对于 http://127.0.0.1:8000/media/truitjes http://127.0.0.1:8000/media/ 给出权限被拒绝:/ media /`。

I can successfully upload pictures in the admin interface and they get stored in /home/jeroen/programming/python/django/sitename/media/truitjes. But When I go to for example http://127.0.0.1:8000/media/truitjes/DSC00068.JPG I get an error: Page not found: /media/truitjes/DSC00068.JPG. Same for http://127.0.0.1:8000/media/truitjes, and ``http://127.0.0.1:8000/media/givesPermission denied: /media/`.

推荐答案

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

Change your media or admin URL / prefix to something different.

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

They cannot have both the same value. If they do, ADMIN_MEDIA_PREFIX has precedence. That means if you try to access http://127.0.0.1:8000/media, you are trying to access the admin media folder.

更改 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}),

这篇关于无法将图像显示在简单的django站点中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆