OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Django Media Files Not Found (404) on Render Deploy

  • Thread starter Thread starter Young Entrepreneur
  • Start date Start date
Y

Young Entrepreneur

Guest
I'm having trouble serving media files on my Django application deployed on Render. The media files are being created and stored (I think) correctly on the server, but accessing them through the browser results in a 404 error. Everything works fine in my local development environment.

Project Setup:

settings.py:


Code:
import os
from pathlib import Path
import environ
import dj_database_url

# Initialize environment variables
env = environ.Env(
    DEBUG=(bool, False)
)

# Read .env file
environ.Env.read_env(os.path.join(Path(__file__).resolve().parent.parent, '.env'))

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Static and Media files
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = '/media/'
MEDIA_ROOT = '/var/data/media'

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

# Static files finders
STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

# Ensure static files are served using Whitenoise
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'allauth.account.middleware.AccountMiddleware',
]

# Whitenoise storage
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

# Other settings...

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

File Existence:

I have verified that the media files are being created on the server:

Code:
render@srv-cpoif12ju9rs738p174g-5f676c9f8c-bwxhd:~/project/src$ ls -l /var/data/media/subtitles/U-sEgjJRHcM_subtitles.json
-rwxr-xr-x 1 render render 14995 Jun 22 14:53 /var/data/media/subtitles/U-sEgjJRHcM_subtitles.json

URL Patterns:

Code:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.auth.views import LoginView, LogoutView
from django.views.generic import TemplateView
from accounts import views as account_views
from subplayer import views as subplayer_views
from allauth.account.views import LoginView, LogoutView, SignupView
from allauth.account.views import PasswordResetView

urlpatterns = [
    path('admin/', admin.site.urls),
...

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

Issue:

Despite the file existing on the server, accessing it via the URL results in a 404 error.

For example: https://xxx.onrender.com/media/subtitles/U-sEgjJRHcM_subtitles.json

What I've Tried:

  • Verified file existence on the server.
  • Checked and double-checked MEDIA_URL and MEDIA_ROOT settings.
  • Ensured URLs are correctly configured.
  • Permissions seem correct with chmod -R 755 /var/data/media.

Additional Info:

  • Using Render for deployment.
  • Created a persistent disk mounted at /var/data.

Any help to resolve this issue would be greatly appreciated!



Feel free to post this on Stack Overflow. It provides all the necessary information for someone to understand your problem and offer potential solutions.
<p>I'm having trouble serving media files on my Django application deployed on Render. The media files are being created and stored (I think) correctly on the server, but accessing them through the browser results in a 404 error. Everything works fine in my local development environment.</p>
<p><strong>Project Setup:</strong></p>
<p><strong><code>settings.py</code>:</strong></p>
<pre class="lang-py prettyprint-override"><code>import os
from pathlib import Path
import environ
import dj_database_url

# Initialize environment variables
env = environ.Env(
DEBUG=(bool, False)
)

# Read .env file
environ.Env.read_env(os.path.join(Path(__file__).resolve().parent.parent, '.env'))

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Static and Media files
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = '/media/'
MEDIA_ROOT = '/var/data/media'

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

# Static files finders
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

# Ensure static files are served using Whitenoise
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'allauth.account.middleware.AccountMiddleware',
]

# Whitenoise storage
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

# Other settings...

# URL configuration
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
</code></pre>
<p><strong>File Existence:</strong></p>
<p>I have verified that the media files are being created on the server:</p>
<pre class="lang-bash prettyprint-override"><code>render@srv-cpoif12ju9rs738p174g-5f676c9f8c-bwxhd:~/project/src$ ls -l /var/data/media/subtitles/U-sEgjJRHcM_subtitles.json
-rwxr-xr-x 1 render render 14995 Jun 22 14:53 /var/data/media/subtitles/U-sEgjJRHcM_subtitles.json
</code></pre>
<p><strong>URL Patterns:</strong></p>
<pre class="lang-py prettyprint-override"><code>from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.auth.views import LoginView, LogoutView
from django.views.generic import TemplateView
from accounts import views as account_views
from subplayer import views as subplayer_views
from allauth.account.views import LoginView, LogoutView, SignupView
from allauth.account.views import PasswordResetView

urlpatterns = [
path('admin/', admin.site.urls),
...

if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
</code></pre>
<p><strong>Issue:</strong></p>
<p>Despite the file existing on the server, accessing it via the URL results in a 404 error.</p>
<p>For example: <a href="https://xxx.onrender.com/media/subtitles/U-sEgjJRHcM_subtitles.json" rel="nofollow noreferrer">https://xxx.onrender.com/media/subtitles/U-sEgjJRHcM_subtitles.json</a></p>
<p><strong>What I've Tried:</strong></p>
<ul>
<li>Verified file existence on the server.</li>
<li>Checked and double-checked <code>MEDIA_URL</code> and <code>MEDIA_ROOT</code> settings.</li>
<li>Ensured URLs are correctly configured.</li>
<li>Permissions seem correct with <code>chmod -R 755 /var/data/media</code>.</li>
</ul>
<p><strong>Additional Info:</strong></p>
<ul>
<li>Using Render for deployment.</li>
<li>Created a persistent disk mounted at <code>/var/data</code>.</li>
</ul>
<p>Any help to resolve this issue would be greatly appreciated!</p>
<hr />
<p>Feel free to post this on Stack Overflow. It provides all the necessary information for someone to understand your problem and offer potential solutions.</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top