how to enable /static/css URL enable in django
-
08-12-2019 - |
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
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:
MEDIA_URL
andMEDIA_ROOT
are for user uploads (FileField
s andImageField
s on your models). It should be its own folder; "media" is common.STATIC_URL
andSTATIC_ROOT
are for your static resources. It should also be its own folder; "static" is common.Do not actually put anything in
STATIC_ROOT
. This directory is only for the output of thecollectstatic
management command in production.Your static resources should go in your apps' "static" folder or a completely new and different directory (i.e. not
MEDIA_ROOT
orSTATIC_ROOT
). You then add the path to that directory toSTATICFILES_DIRS
.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.