gpt4 book ai didi

django - 无法在简单的 django 站点中显示图像

转载 作者:行者123 更新时间:2023-12-04 21:54:55 25 4
gpt4 key购买 nike

我有一个非常简单的 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.JPG .与 http://127.0.0.1:8000/media/truitjes 相同, 和`` http://127.0.0.1:8000/media/ gives权限被拒绝:/media/`。

最佳答案

将您的媒体或管理员 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}),

关于django - 无法在简单的 django 站点中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2148738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com