Search code examples
pythondjangodjango-staticfiles

I started a Django project with all requirements but static files can't load successfully


I'm working with Python 3.8.3, pip 20.1.1, Django 3.0.6 and virtual environment are already installed but I'm not getting the style from my CSS files and the web page behavior from JavaScript onto my web page and here is the photo of errors in my console


Error from console


In my settings I configured well all about the static file and the template and the following is my

settings.py

STATICFILES = [
    os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')

and that's also the the URLs for the whole projects

urls.py

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

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

and I done well all about the URL configuration and the following is my app URL

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home')
]

and that's the views which return the page

views.py

from django.shortcuts import render
from django.http import HttpResponse


def home(request):
    return render(request, 'index.html')

I don't know if there is any error in my codes but I can't get the website as I expected and I want to know if there is any fixation of this because I met with it couple times.


Solution

  • if you do it for your debug server, you can add this lines to your urls.py

    from django.conf.urls.static import static
    ...
    if settings.DEBUG:
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
                  static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)