0

What I want to do is make all necessary changes in settings.py for Production purpose.When I set DEBUG=True,Everything works all right but when I set DEBUG=False,It makes me feel so tired and depressed.I have been trying for many days but could't figure out.Setting DEBUG=False, Static files runs and some don't but mediafiles completely stop working and i get Server Error (500) in some of the pages.And,I know the fix is in settings.py but don't know how to?


import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

SECRET_KEY = ')osa2y(^uk4sdghs+#(14if-)b1&6_uo@(h#0c%sci^a#!(k@z'

DEBUG = False

ALLOWED_HOSTS = ['dimensionalillusions.herokuapp.com','127.0.0.1']


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    
    #CUSTOM  APPS
     'EHub.apps.EhubConfig',
     'EBlog.apps.EblogConfig',

     'EDashboard.apps.EdashboardConfig',
     
     'mptt',
     'ckeditor',
     'taggit',

]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    

    'whitenoise.middleware.WhiteNoiseMiddleware', 
        
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'Dimensionalillusions.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        
        'DIRS': [os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'templates'),],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]


WSGI_APPLICATION = 'Dimensionalillusions.wsgi.application'


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}



AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/staticfiles/'  


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


STATIC_ROOT=os.path.join(BASE_DIR, 'static')

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

MEDIA_ROOT =os.path.join(BASE_DIR, 'staticfiles/mediafiles')  
MEDIA_URL ='/mediafiles/'  


EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587

EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''



Sam
  • 19
  • 3

2 Answers2

0

you should change in url for read media and static link (some static load from chrome cache and your static not load in debug mode)

for load this use this code in urls.py

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
  • these are only for local development when you deploy on heroku and set DEBUG=False urlpatterns won't work. In production it won't work .Can you help me in this – Sam Jun 27 '21 at 14:20
  • you can set STATIC_ROOT to public_html/static in settings and run command python manage.py collectstatic – pouria farhadi Jul 07 '21 at 10:58
  • Thanks.By the ways,Do you know how many objects can a Django class model handle smoothly?.Does Huge objects(i.e +9999 objects) creation from a single model affect in performance? – Sam Jul 08 '21 at 13:34
  • Massive data performance directly related to database performance – pouria farhadi Jul 11 '21 at 04:42
0

Add MEDIA_URL and MEDIA_ROOT to your projects urls.py

from django.conf.urls.static import static

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Ram
  • 4,724
  • 2
  • 14
  • 22
  • these are only for local development when you deploy on heroku and set DEBUG=False urlpatterns won't work – Sam Jun 27 '21 at 14:18
  • Where are you storing your media files ? Are you using something like Amazon S3 ? – Ram Jun 27 '21 at 14:50
  • No,I am not.I will use it later but i want to know if somehow i could serve mediafiles without using Amazon s3,cloudflare.Is it possible to ? or you need to rely on Cloudflare,Amazon for media files? – Sam Jun 27 '21 at 15:41
  • Did you deploy your project to Heroku ? – Ram Jun 27 '21 at 15:45
  • yes,I did it serves staticfiles but not media files with above settings.py.Heroku provides config vars but i don't know if it's gonna work. – Sam Jun 27 '21 at 15:50
  • Heroku doesn't store your media files forever. The Heroku filesystem is ephemeral - that means that any changes to the filesystem whilst the dyno is running only last until that dyno is shut down or restarted. So you need to use Amazon S3 etc., Read this: https://stackoverflow.com/questions/12123050/no-permanent-filesystem-for-heroku – Ram Jun 27 '21 at 15:54
  • Do you know some free technologies that allows you to store mediafiles for free.I just want to test – Sam Jun 28 '21 at 04:51
  • AWS has a free Tier plan where they provide S3 for free. You can check that out [here](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all) – Ram Jun 28 '21 at 05:52