0

I'm using namecheap shared hosting and I hosted my site using cpanel. My site is working fine but if i made DEBUG = False in project settings.py file the static files are not loading. My site url: https://drshahidabegum.com/

settings.py

-----------

# Static files (CSS, JavaScript, Images)
STATIC_DIR = [
    BASE_DIR / "static",
]


STATIC_URL = '/static/'
STATICFILES_DIRS = [
    BASE_DIR / "static",
]

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

urls.py in project folder

-------------------------

"""
from django.contrib import admin
from django.urls import path, include

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


# Configure Admin Titles

admin.site.site_header = "DR. Shahida Begum"
admin.site.site_title = "DR. Shahida Begum"
admin.site.index_title = "Welcome to Admin Dashboard"
handler404 = 'dr_shahida.views.error_404_view'

urls.py in app folder

-------

from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', views.index, name='index'),
    path('testimonial/', views.testimonial, name='testimonial'),
    path('contact/', views.contact, name='contact'),
    path('blog/', views.blog, name='blog'),
    path('blog/post/<int:post_id>', views.blogdetailview, name='blogdetailview'),
    path('set_language/<str:ln>', views.set_lang, name='set_lang'),

]

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

I want my static files to work when DEBUG = False in project settings.py file.

  • Does this answer your question? [Why does DEBUG=False setting make my django Static Files Access fail?](https://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail) – Ivan Starostin Jan 29 '23 at 20:01
  • This worked for static but after following this media files are not working. – Abdullah Al Azim Jan 30 '23 at 03:39

1 Answers1

0

With DEBUG set to False follow these steps:

  1. in settings set the static root to public_html folder:
STATIC_URL = 'static/'
STATICFILES_DIRS = [
    BASE_DIR / 'static'
]
STATIC_ROOT = '/home/your_user_path/public_html/static'
MEDIA_URL = '/media/' 
MEDIA_ROOT = '/home/your_user_path/public_html/media'
  1. go to terminal activate env and run python manage.py collect static
  2. restart app

*note: ending of staticroot should be same as static url

Kawsu
  • 1
  • 1