无法获取要在simple django站点中显示的图像
我有一个非常简单的django站点,但是我很难在管理面板中显示上传的图像 my无法获取要在simple django站点中显示的图像,django,image,Django,Image,我有一个非常简单的django站点,但是我很难在管理面板中显示上传的图像 mysettings.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 med
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/'
我的url.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}),
)
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/dsc0068.JPG
。http://127.0.0.1:8000/media/truitjes
,并且`给予
拒绝的权限:/media/`。将您的媒体或管理员URL/前缀更改为其他内容
它们不能具有相同的值。如果他们这样做,ADMIN\u MEDIA\u前缀
具有优先权。这意味着如果您尝试访问http://127.0.0.1:8000/media
,您正在尝试访问管理媒体文件夹
更改管理\u媒体\u前缀
:
ADMIN_MEDIA_PREFIX = '/admin-media/'
或更改媒体根目录
:
# 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}),
#在settings.py中
MEDIA_ROOT='/home/jeroen/programming/python/django/ninikske/static'
媒体http://127.0.0.1:8000/static/'
#在url.conf中
(r'^static/(?P.*)$,'django.views.static.service',
{'document_root':settings.MEDIA_root}),
在Django 1.3中,如果您只是在开发过程中尝试在视图中显示图像,请执行以下步骤:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
#..rest of url.py config...
urlpatterns += staticfiles_urlpatterns()
<img src="{{ STATIC_URL }}pic.png" />
请看这里的开发中的静态文件服务:我想在这个非常好的答案中添加一点:如果您有全局的静态文件,并且位于我在仔细阅读文档后发现的常规“静态”目录中,您可以使用以下代码将该目录添加到STATICFILES\u DIRS元组:os.path.join(os.path.realpath(os.path.dirname(file)),“static”),-例如,这对CSS很有用。