简体   繁体   English

手动检查时即使文件存在也无法访问媒体文件

[英]Not able to access media file even if file exists when checked manually

I have a media file that Django isn't able to access, even if it definitely exists if I enter its URL in the browser.我有一个 Django 无法访问的媒体文件,即使我在浏览器中输入它的 URL 它肯定存在。

Info信息

  1. Language : Python [v.3.9.7]语言:Python [v.3.9.7]
  2. Platform : Django [v.3.2.8]平台:Django [v.3.2.8]

Goal目标

Being able to access the media file能够访问媒体文件

Description描述

The thing is, people can upload books on my website.问题是,人们可以在我的网站上上传书籍。 People upload images of these books in different sizes, so I want to be able to handle all of them, so I access the width and height of the image and then use that as the size of the image in the view (CSS).人们上传这些书的不同尺寸的图像,所以我希望能够处理所有这些,所以我访问图像的宽度和高度,然后将其用作视图中图像的大小(CSS)。 To do that, I've used a custom filter image_size that is going to do all the work of accessing the image and finding its size, which I send back to the view:为此,我使用了一个自定义过滤器image_size ,它将完成访问图像和查找其大小的所有工作,并将其发送回视图:

@register.filter
def image_size(img, host):
    img = host[0] + "://" + host[1] + img
    with Image.open(img) as image:
        width, height = image.size
        dct = {
            "width": width,
            "height": height
        }
        return dct

Here, host[0] is the protocol ( request.scheme ) of the URL of the image and host[1] is the host name ( request.get_host() ).这里, host[0]是图像的 URL 的协议( request.scheme ), host[1]是主机名( request.get_host() )。 Here's my view:这是我的看法:

def all_books(request):
    if request.user.is_authenticated:
        context = {
            "books": Book.objects.all(),
            "request": request,
            "user_profile": UserProfileInfo.objects.get(user=request.user),
            "mail_sent": False,
        }
    else:
        context = {
            "books": Book.objects.all(),
            "request": request,
            "mail_sent": False,
            "user_profile": False,
        }
    context["host"] = [request.scheme, request.get_host()]
    if request.GET.get("context") == "mail_sent":
        context["mail_sent"] = True
    return render(request, 'main/all_books.html', context)

settings.py static and media config : settings.py static 和媒体配置

STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
MEDIA_ROOT  = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
LOGIN_URL = '/main/user_login/'

Where BASE_DIR is Path(__file__).resolve().parent.parent ( Path is imported from pathlib )其中BASE_DIRPath(__file__).resolve().parent.parentPath是从pathlib导入的)

I haven't done collectstatic because for some reason it removes connection to CSS and suddenly no CSS is rendered.还没有完成collectstatic ,因为由于某种原因它删除了与 CSS 的连接,并且突然没有呈现 CSS 。

What should I do to get it working?我应该怎么做才能让它工作?

Uploaded files go to MEDIA_ROOT unless it is defined differently in the model field You build the path to the img file "manually" in the filter... which does not point to media/...上传文件 go 到 MEDIA_ROOT 除非它在 model 字段中定义不同 您在过滤器中“手动”构建 img 文件的路径......它不指向媒体/......

img = host[0] + "://" + host[1] + img

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

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