Skip to main content

Django static files not working when debug false || debug true

Django static not working when debug is false

# Django static and media files not working when debug is false

In this article you will learn how to fix problem of not loading static files and media in Django even the DEBUG is FALSE. This is the easiest and safest solution.

# Problem:

Django static and media files not working when debug is false

 ➤ Code: settings.py

  • DEBUG = False #True

  • ALLOWED_HOSTS = ['*'] #Host name

# Problem Fix:

Let's see, How you can fix the problem of Django static and media files not working when DEBUB = False :

1.)First way: devserver in insecure mode

If you still need to server static locally (e.g. for testing without debug) you can run devserver in insecure mode:

  • python manage.py runserver --insecure

--insecure: it means you can run server without security mode.

Note: This is Exactly you must type on terminal to run your project without "DEBUG = TRUE" and then you see all assets (static) file is loading correctly On local server.

2.)Second way: devserver in secure mode

Step 1 : Define a STATIC_ROOT and MEDIA_ROOT path in settings.py.

 ➤ Code: settings.py

  1. STATIC_URL = '/static/'
    MEDIA_URL = '/media/'

    if DEBUG:
    STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
    else:
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')

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

Step 2 : Now define your URLConf that if static is in the beginning of url, access files from the static folder staticfiles.

 ➤ Code: urls.py

  1. from django.conf.urls import url
    from django.conf import settings
    from django.views.static import serve

    urlpatterns = [
    url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
    url(r'^static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT}),
    ]

Note: This is your project's urls.py file.

Step 3 : Now you can run your django project.

  • python manage.py runserver

Note : Congratulations, now static and media files will be loaded in your Django project when DEBGU = False. Have fun!

Comments

Sami said…
this actually not work.
PLease help
contact: saitwalsamir@gmail.com
As a leading Top Mobile App Development Company, Appsinvo company is well known to design and build customized web and mobile app solutions for clients.
Complete integration of the mobile app and the website gives the businesses a boost not only in their growth but also in the revenue.
Top Mobile App Development Company in Delhi
Unknown said…
Thank God. It works like Magic. I am saved today
Unknown said…
A big thanks, after the trouble of a whole day, your post helped me too much.
Thanks again
Unknown said…
after following these steps this error is showing ...

cannot import name 'url' from 'django.conf.urls'
Vineet Singh said…
I tried this and it works like charm but I'm facing only one issue after following this article my website static files are working as expected but django admin panel static files not working and admin panel appears only in simple htmls not static file rendering, can you please help me with this.
devtom said…
not working if your site is on production server it only works for local
machine server
Unknown said…
Need to change Nging to add location:
location /media/ {
alias /home/xuser/xproject/media/;
expires 1d;
}
https://dsbyprateekg.blogspot.com/p/python-basics-lists.html
oums said…
thanks bro .

django.conf.urls.url() was deprecated in Django 3.0, and is removed in Django 4.0+.
so you only have to update the import and replace url with re_path.

Popular Posts

How to remove the date and .html from every blogger post url

#Remove date and .html from blogger post url A Common search term which every blogger search is How to Remove Date From Blogger Post URL or how do I remove date from blogger permalink? Follow the steps below and then date and .html will be removed from the URL of your blogger post. Step 1 : Login to your Blogger blog and select Theme / Template. Step 2 : Click on Edit HTML and paste the below code just above the </head> tag let's see code :   ➤ Code : mycode.js; Copy code <script type='text/javascript' > //<![CDATA[ // BloggerJS v0.3.1 var urlTotal,nextPageToken,postsDatePrefix=!1,accessOnly=!1,useApiV3=!1,apiKey="",blogId="",postsOrPages=["pages","posts"],jsonIndex=1,secondRequest=!0,feedPriority=0,amp="&"[0];function urlVal(){var e=window.location.pathname,t=e.length;return

how to create enum in java

What is enum in Java? An enum is a special "data type" that used to create our own datatype like classes and It represents a group of constants (a variable that does not change). The enum is short for "enumerations", which means "specifically listed" and the Enum data also known as Enumerated Data Type. # Defining Java enum Instead of C/C++, in java programming language enum types are more powerful. Here, we can define an enum both inside and enum outside the class but not inside a Method "Rules" to defing enum in Java: To create an enum in Java, use the enum keyword (instead of class or interface) , and separate the constants with a comma (,) . The enum can be defined within or outside the class because it is similar to a class. The semicolon (;) at the end of the enum constants are optional Because enum are constants, the names of an enum type's fields are in uppercase letters.