简体   繁体   English

找不到页面 404 Django 媒体文件

[英]Page not found 404 Django media files

I am able to upload the files to media folder( '/peaceroot/www/media/' ) that I have set up in settings.py as below我可以将文件上传到我在settings.pysettings.py媒体文件夹( '/peaceroot/www/media/' ),如下所示

MEDIA_ROOT = '/peaceroot/www/media/'
MEDIA_URL = '/media/'

But through admin I tried to access the uploaded image file但是通过管理员我试图访问上传的图像文件

http://localhost:8000/media/items/1a39246c-4160-4cb2-a842-12a1ffd72b3b.jpg http://localhost:8000/media/items/1a39246c-4160-4cb2-a842-12a1ffd72b3b.jpg

then I am getting 404 error.然后我收到 404 错误。

The file exists at peaceroot/www/media/items/1a39246c-4160-4cb2-a842-12a1ffd72b3b.jpg该文件存在于peaceroot/www/media/items/1a39246c-4160-4cb2-a842-12a1ffd72b3b.jpg

Add media url entry in your project urlpatterns:在您的项目 urlpatterns 中添加媒体 url 条目:

from django.conf.urls.static import static
from django.conf import settings

...
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The better way for MEDIA_ROOT is, MEDIA_ROOT 的更好方法是,

try to make media path dynamic will be easy when you shift your project.当您转移项目时,尝试使媒体路径动态化会很容易。

Settings.py设置.py

BASE_DIR = os.path.dirname(os.path.dirname(__file__))


MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')
MEDIA_URL = '/media/'

urls.py网址.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Look at this看看这个

https://docs.djangoproject.com/en/dev/howto/static-files/ https://docs.djangoproject.com/en/dev/howto/static-files/

Just to add: in case the other answers do not work for you, try putting the static url before the other ones.补充一点:如果其他答案对您不起作用,请尝试将静态 url 放在其他答案之前。 Like so:像这样:

urlpatterns = static(...) + [...]

What may be happening is that some of your patterns in the list prevent the request from reaching the static handlers.可能发生的情况是列表中的某些模式阻止请求到达静态处理程序。 So putting the static handlers first solves this.所以首先放置静态处理程序可以解决这个问题。 Worked for me.为我工作。

In my development server I fixed it by commenting out these lines in settings.py在我的开发服务器中,我通过在 settings.py 中注释掉这些行来修复它

STATICFILES_DIRS = (
     os.path.join(BASE_DIR, 'static'),
)

In template create link by anchor tag and add .url in the end to that fileobject eg在模板中通过锚标记创建链接并在最后添加 .url 到该文件对象,例如

{% for post in post %}

   <a href="{{post.imagefilename.url}}" > 

{% endfor %}

This is a server error.这是服务器错误。 I'm assuming you are using Nginx.我假设您正在使用 Nginx。 Just add this in your Nginx Configuration file(/etc/nginx/sites-available/example.com) just under location /static/只需将其添加到您的 Nginx 配置文件(/etc/nginx/sites-available/example.com)中,就在位置 /static/ 下

location /media/ {
    root /home/user/myprojectdir;
}

Here, user should be your username you created and myprojectdir should be your project directory .在这里, user应该是您创建的用户名myprojectdir应该是您的项目目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM