Question

When I use MEDIA_URL or STATIC_URL to point to /static/ currently set MEDIA_URL to /static/ and using it in a path to CSS file like:

<link rel="stylesheet" type="text/css" href="{{MEDIA_URL}}css/css.css" />

It points to /static/css.css, but trying http://localhost/static/css.css gives 404 error.

I have in settings:

.....
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/static/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = 'D:/programming/django_projects/ecomstore/'
.....

In urls.py I have point to static like this:

url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
     {'document_root':'D:/programming/django_projects/ecomstore/'}

So where problem is? Why it is showing 404, do I need to create some view for static files? Or is there some thing else wrong in my settings or urls.py ? Any response will be appreciated, as I am newbie in django.

thanks in advance

Was it helpful?

Solution

You need to re-read the docs more closely: https://docs.djangoproject.com/en/dev/howto/static-files/

Here's some things to note:

  1. MEDIA_URL and MEDIA_ROOT are for user uploads (FileFields and ImageFields on your models). It should be its own folder; "media" is common.

  2. STATIC_URL and STATIC_ROOT are for your static resources. It should also be its own folder; "static" is common.

  3. Do not actually put anything in STATIC_ROOT. This directory is only for the output of the collectstatic management command in production.

  4. Your static resources should go in your apps' "static" folder or a completely new and different directory (i.e. not MEDIA_ROOT or STATIC_ROOT). You then add the path to that directory to STATICFILES_DIRS.

  5. Don't add anything to urls.py. In development, Django will automatically serve anything in your apps "static" directories or any directory in STATICFILES_DIRS. In production, your webserver will be responsible for serving these files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top