第一次发帖,所以要温柔。
我正在尝试在 django 中显示静态图像。我可以将图像上传到我的媒体目录,但是当我尝试显示它时,我什么也没有得到。如果我将网址复制到浏览器,我会得到...
找不到页面 (404) 请求方式:GET 请求网址:http://127.0.0.1:8000/media/Will_and_Matt.jpg 提出者:django.views.static.serve
详细信息.py
...
<img src="{{ student.photo.url }}" class="img-responsive">
...
模型.py
class Student(models.Model):
photo = models.FileField( blank=True, null=True)
项目url.py
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^opencmis/', include('opencmis.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_URL)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_URL)
设置.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
我的更新表单在创建媒体文件夹并将图像放入其中时起作用。
URL 模式似乎与/media 匹配,因为它不会给出错误,只是不显示图像。
有什么想法吗?
最佳答案
document_root=settings.STATIC_URL
应为 document_root=settings.STATIC_ROOT
且与 MEDIA_ROOT
类似。您需要其中的文件系统路径,而不是 URL。
--dhke
关于python - 找不到页面 django.views.static.serve,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38930474/