admin管理员组

文章数量:1123649

Hopefully a simple issue that I cannot find any answer to online so far.

Background : I have an django web app that is hosted on render. Everything is working apart from the media files that are hosted on a bought disk (mounted at /var/data) for my project.

I have followed the documentation and added the following to settings.py:

MEDIA_URL = '/media/'
MEDIA_ROOT = '/var/data'   

media context is set:

           "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
                "django.template.context_processors.media",
            ],

I have urlpatterns configured for media:

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

whitenoise is configured:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

requirements.txt

asgiref==3.8.1
click==8.1.8
dj-database-url==2.3.0
Django==5.1.4
django-crispy-forms==2.3
gunicorn==23.0.0
h11==0.14.0
packaging==24.2
pillow==11.0.0
psycopg2-binary==2.9.10
sqlparse==0.5.3
typing_extensions==4.12.2
uvicorn==0.34.0
whitenoise==6.8.2

index.html


{% extends "airfryer_app/base.html" %}

{% block body %}



  <div class="product-container flex flex-wrap justify-content-center">


    {% for recipe in recipes %}
      <div class="product shadow-lg w-1/5 rounded-lg m-10">
        <div class="product-image">
          <img src="{{ recipe.image.url }}" alt="">
        </div>

        <div class="p-5">
          <div class="font-bold">
            {{ recipe.name }}
          </div>
          <div>
            {{ recipe.description }}
          </div>
          <div class="text-orange-700 font-bold text-orange">
            {{ recipe.time }} minutes
          </div>
          <div class="mt-5">
            <a class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded"
               href="{% url 'recipe' recipe.id %}">View Details</a>
          </div>
        </div>
      </div>
      {% if forloop.counter|divisibleby:3 %}
        <div class="w-full"></div>
      {% endif %}
    {% endfor %}
  </div>
{% endblock %}

Using the web app I can upload user images to /var/data but when In try to retrieve them I get the below errors:

10.217.41.72 - - \[09/Jan/2025:13:20:43 +0000\] "GET /media/croissant.jpg HTTP/1.1" 404 179 "/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
10.217.185.86 - - \[09/Jan/2025:13:20:43 +0000\] "GET /media/bacon.jpg HTTP/1.1" 404 179 "/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"

The image files are uploaded to the correct location.

Also, when I to resolve url (.jpg) I get a 404 error

Not Found
The requested resource was not found on this server.
10.217.209.166 - - \[09/Jan/2025:13:23:19 +0000\] "GET /media/croissant.jpg HTTP/1.1" 404 179 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"

Using the web app I can upload user images to /var/data but when In try to retrieve them I get the below errors:

10.217.41.72 - - \[09/Jan/2025:13:20:43 +0000\] "GET /media/croissant.jpg           HTTP/1.1" 404 179 "/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
10.217.185.86 - - \[09/Jan/2025:13:20:43 +0000\] "GET /media/bacon.jpg HTTP/1.1" 404 179 "/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"

Also, when I to resolve url (.jpg) I get a 404 error

Not Found
The requested resource was not found on this server.

10.217.209.166 - - \[09/Jan/2025:13:23:19 +0000\] "GET /media/croissant.jpg HTTP/1.1" 404 179 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"

Does anybody have any insight into what may be causing this issue ?

Any help is greatly appreciated.

Thanks

本文标签: retrieving media files on render with djangoStack Overflow