Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you serve static files when using the django runserver development server?

Tags:

django

I am using django 1.5

I am able to serve the files in production because it is being handled at the apache level. Here is my httpd.conf file:

<VirtualHost *:80>
WSGIScriptAlias / /home/membership/membership/wsgi.py

Alias /static/ "/home/membership/static/"


<Directory /home/membership/static>
Order deny,allow
Allow from all
</Directory>

<Directory "/usr/lib/python2.6/site-packages/django/contrib/admin/static/admin">
    Order deny,allow
    Allow from all
</Directory>

<Directory /home/membership/membership>
<Files wsgi.py>
Order deny,allow
Satisfy Any
Allow from all
</Files>
</Directory>
</VirtualHost>

This works fine in production because of the Alias /static/ "/home/membership/static/"

When I try and run the app in my local development environment I can't get it to serve static files I just get a page not found 404 error. I am guess that this is because when I develop locally the requests are going straight to the development server since apache is not being used.

enter image description here

I do have a file at /static/me.png .

Is there somewhere that I am supposed to specify to serve static files in development?

When running python manage.py collectstatic it seems to collect only the static files for the admin app. I have a file directly in the /app/static directory that I am trying to serve.

like image 825
Spencer Cooley Avatar asked Jul 18 '13 02:07

Spencer Cooley


2 Answers

If you're Serving static files during development

On your settings.py file:

# Add it on your settings.py file
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"), # your static/ files folder
]

Example:

enter image description here

On your root urls.py file:

# add these lines
from django.conf import settings
from django.conf.urls.static import static

# Add +static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Example:

enter image description here

This is not suitable for production use! See more: https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-during-development

For media files on media/ directory: https://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user-during-development

like image 152
Fred Sousa Avatar answered Oct 10 '22 05:10

Fred Sousa


Did you define the path to your static files in your site's settings? I don't mean the url /static/, I mean the STATICFILES_DIR (it's what tells your devserver where the static files are just like the config file tells the apache)

It's really best to just follow the documentation, it is absolutley fantastic:

Documentation

like image 34
yuvi Avatar answered Oct 10 '22 05:10

yuvi