Question:
How to solve Static Files not being found in Django?

If static files are not being found in your Django application, there could be an issue in STATIC_URL in settings.py:


  1. Check STATIC_URL in settings.py:

Ensure that you have the correct STATIC_URL in your settings.py file. It should point to the URL where your static files are served.


# settings.py

STATIC_URL = '/static/'


  1. Also, check if the DEBUG = False

If you are getting DEBUG = False, Django will not handle static files. In this case you have to try the below codes to fix the issue. 


Inside your MyProject/MyProject2/urls.py file, you can add another URL pattern to look for static files under the URL /static/*:

from django.views.static import serve 

from django.conf import settings

from django.conf.urls.static import static


urlpatterns = [

    ...,

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

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


If you get mimetype errors of anytype, like I have had before in the past, you can import mimetypes in your MyProject/MyProject2/settings.py file:

import mimetypes 

mimetypes.add_type("text/css", ".css", True)


Also, if you still want to set DEBUG = False and serve static files locally for testing, you can runserver in insecure mode:


python3 manage.py runserver --insecure


Answered by:> apache

Credit:> Stackoverflow


Suggested reads:

>Sending Audio file from Django to Vue

>How to keep all query parameters intact when changing page in Django?

>Solved: TaskList View in Django

>Implement nested serializers in the Django rest framework

>Define blade component property in server - Laravel



Ritu Singh

Ritu Singh

Submit
0 Answers