Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: media files on production #1

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ exclude =
manage.py,
settings.py,
wsgi.py,
venv/
venv/,
*/config/*
8 changes: 7 additions & 1 deletion django_project/config/production.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from .base import *

DEBUG = False
Expand All @@ -18,5 +19,10 @@
'PORT': DB_PORT,
}
}

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
SECRET_KEY = os.getenv("SECRET_KEY", "")
SECRET_KEY = os.getenv("SECRET_KEY", "")

MEDIA_ROOT = os.path.join("/var/www/","djangoblogs.onrender.com/media/")

os.makedirs(MEDIA_ROOT, exist_ok=True)
12 changes: 11 additions & 1 deletion django_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import include, path

from django.views.static import serve
from django.urls import re_path
from django.conf import settings

from users import views as user_views

Expand All @@ -40,6 +42,14 @@
path("", include("blog.urls")),
]

if not settings.DEBUG:
# deployment fix to server media files
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve,{
'document_root': settings.MEDIA_ROOT,
})
]

if settings.DEBUG:
urlpatterns += static(
settings.MEDIA_URL,
Expand Down
12 changes: 9 additions & 3 deletions users/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from django.test import TestCase # noqa
from django.test import TestCase
from django.contrib.auth.models import User

from django.conf import settings
Expand All @@ -8,7 +8,12 @@

class TestRegisterView(TestCase):
@classmethod
def setUp(cls):
def setUpTestData(cls):
if(os.path.exists(os.path.join(settings.MEDIA_ROOT, "users", "random"))):
os.rmdir(os.path.join(settings.MEDIA_ROOT, "users", "random"))

@classmethod
def tearDownClass(cls):
if(os.path.exists(os.path.join(settings.MEDIA_ROOT, "users", "random"))):
os.rmdir(os.path.join(settings.MEDIA_ROOT, "users", "random"))

Expand Down Expand Up @@ -43,7 +48,8 @@ def setUpTestData(cls):
def tearDownClass(cls):
media_dir = settings.MEDIA_ROOT
username = cls.user.username
os.rmdir(os.path.join(media_dir, 'users', username))
if(os.path.exists(os.path.join(media_dir, 'users', username))):
os.rmdir(os.path.join(media_dir, 'users', username))

def test_get_login_view(self):
response = self.client.get("/login/")
Expand Down
Loading