diff --git a/.flake8 b/.flake8 index 9ee0788..8f6cb65 100644 --- a/.flake8 +++ b/.flake8 @@ -5,4 +5,5 @@ exclude = manage.py, settings.py, wsgi.py, - venv/ + venv/, + */config/* diff --git a/django_project/config/production.py b/django_project/config/production.py index 70f9c0c..62aaebc 100644 --- a/django_project/config/production.py +++ b/django_project/config/production.py @@ -1,3 +1,4 @@ +import os from .base import * DEBUG = False @@ -18,5 +19,10 @@ 'PORT': DB_PORT, } } + STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' -SECRET_KEY = os.getenv("SECRET_KEY", "") \ No newline at end of file +SECRET_KEY = os.getenv("SECRET_KEY", "") + +MEDIA_ROOT = os.path.join("/var/www/","djangoblogs.onrender.com/media/") + +os.makedirs(MEDIA_ROOT, exist_ok=True) \ No newline at end of file diff --git a/django_project/urls.py b/django_project/urls.py index c1dae35..d0a0095 100644 --- a/django_project/urls.py +++ b/django_project/urls.py @@ -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 @@ -40,6 +42,14 @@ path("", include("blog.urls")), ] +if not settings.DEBUG: + # deployment fix to server media files + urlpatterns += [ + re_path(r'^media/(?P.*)$', serve,{ + 'document_root': settings.MEDIA_ROOT, + }) + ] + if settings.DEBUG: urlpatterns += static( settings.MEDIA_URL, diff --git a/users/tests.py b/users/tests.py index 35e5568..7f11130 100644 --- a/users/tests.py +++ b/users/tests.py @@ -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 @@ -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")) @@ -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/")