Django runserver not serving static files in development

24,382

Solution 1

I managed to fix it.

I created another directory called static in my project folder called static, and copied there my static files.

I then added:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
import settings
if settings.DEBUG:
    urlpatterns += staticfiles_urlpatterns()

to my urls.py

and

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

to my settings.py.

Then, when I deploy I execute manage.py collectstatic and since Apache is configured properly, everything will work!

Based on http://dlo.me/archives/2013/01/14/how-to-serve-static-files-django/

Thanks to all.

Solution 2

I usually run python manage.py runserver --insecure to force serving of static files with the staticfiles app even if the DEBUG setting is False.

Here's the link of their documentation. https://docs.djangoproject.com/en/3.2/ref/contrib/staticfiles/#cmdoption-runserver-insecure

Share:
24,382

Related videos on Youtube

Meir
Author by

Meir

Updated on July 11, 2022

Comments

  • Meir
    Meir almost 2 years

    I am using Django with runserver for my development. When I deploy to my production server I can see all my static files, but not on my local computer.

    I did collectstatic and I have set DEBUG = True.

    I found many different opinions online, the most prominent being the STATICFILES_DIRS, but that does not work for me.

    How can I set it so that in my development environment I can see the static files, and when I upload my files to the server I do not need to make any changes for the production environment to work properly.

    Edit - my urls.py file:

    from django.conf.urls import patterns, include, url
    from django.conf.urls.static import static
    import newsflashes
    import settings
    
    from django.contrib import admin
    admin.autodiscover()
    
    urlpatterns = patterns('',
        url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
        url(r'^admin/', include(admin.site.urls)),
        url(r'^', include('newsflashes.urls')),
    ) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    

    Edit - file structure:

    I have two directories, static and dynamic. Inside static are the static files, and in the dynamic directory are the django apps.

    Edit - Settings:

    My relevant settings are as follows

    STATIC_ROOT = os.path.join(BASE_DIR, '..', 'static')
    STATIC_URL = '/static/'
    STATICFILES_DIRS = ()
    
    • Timmy O'Mahony
      Timmy O'Mahony over 10 years
      Have you followed the documentation and added the appropriate lines to your urls.py
    • Meir
      Meir over 10 years
      Yes, I added the urls file to the question
    • Lukasz Koziara
      Lukasz Koziara over 10 years
      You should tell us something about your settings (STATIC_ROOT, STATIC_URL, STATICFILES_DIRS) and static files location
    • Meir
      Meir over 10 years
      Added to the question above. Should I add anything else?
  • Grant Eagon
    Grant Eagon about 9 years
    To clarify, in settings.py make sure DEBUG is set to true, then in urls.py add the urls.py code above.
  • Raoul
    Raoul almost 9 years
    I only had to point STATICFILES_DIRS to my (source) static file dirs. After that, running collectstatic collected the files, and after that runserver would serve them. Thanks for the hint :)
  • Yeo
    Yeo almost 9 years
    This has already been included by default in the django.contrib.staticfiles.urls
  • Udemezue John
    Udemezue John over 2 years
    Thanks, I am sorry about that, I have deleted and upvoted