运行Django 2.1.5与Python 3.7.2 32位,Win10 localhost,没有nginx等克隆repo https://github.com/axelpale/minimal-django-file-upload-example.git
可以上传文件,它们在数据库中显示为URL链接,在页面上显示为列表项,但图像/文件不显示在页面上,并在控制台中返回404。
返回错误:GET http://127.0.0.1:8001/media/documents/2019/04/07/image.jpg 404 (Not Found)
我可以在资源管理器中的该位置看到该文件。
代码完全按照repo添加以下内容:
list.html第14行
<li><img src="{{ document.docfile.url }}" height="60px"></li>
和
URLs.朋友
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + [ path('', views.list, name='list')]
我尽可能地遵循文档,并查看其他SO帖子,建议静态模式应该先行。
应该注意,媒体文件夹位于目录的顶层。
for_django_2-0
-我的项目
- 我的项目
- 媒体
--myapp
另外:DEBUG = True
尝试过:python manage.py runserver 8001 --insecure
和python manage.py runserver 8001
任何帮助将非常感激。
谢谢,乔恩
为了回答我自己的问题,在进一步挖掘另一个主题后,我发现了这个有用的视频。
https://www.youtube.com/watch?v=PIvlcmnayOE&t=55s
该视频解释了我在其他教程中遗漏的内容。您的静态网址路径需要位于根项目urls.py中,而不是应用程序urls.py
MyProject的/ urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + [
path('admin/', admin.site.urls),
path('myapp/', include('myapp.urls'))
这现在有效。