Reputation: 1358
Here is the directory structure pf project.
mysite
\media
\static
\images
\css
base.css
\js
\templates
base.html
\settings.py #and other .py files
Now say if i have a file C:\mysite\static\css\base.css
or an image C:\mysite\static\images\ball.png
.How am i going to serve it using django development server. I know this is not the recomment way, but just in development i want this to work. I have read the docs and other answers on stackoverflow but they don't seem to work. May be i am being confused by media
and static
and urlpatterns
. I have spent hours trying to figure this out but still no luck.
I am new to django and all this seems to me just an overkill to get a css or image showing up.
What changes or editing do i need to make for this to work. How will i referece the image and css in template?
Upvotes: 2
Views: 5657
Reputation: 6503
Here is the how to for static files from the Django documentation that takes you through the whole process step-by-step.
Add the following to your urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
Then 'This view is automatically enabled and will serve your static files at STATIC_URL when you use the built-in runserver management command. ... This will inspect your STATIC_URL setting and wire up the view to serve static files accordingly. Don't forget to set the STATICFILES_DIRS setting appropriately to let django.contrib.staticfiles know where to look for files additionally to files in app directories.'
It is worth mentioning that this is most definitely inappropriate for deployment, but will work well for development.
Upvotes: 4
Reputation: 239200
The problem is that you're not supposed to manually put anything in STATIC_ROOT
. It's only a dumping ground for the collectstatic
management command in production, and is supposed to be served by your webserver.
In development, Django automatically serves anything in your apps' "static" directories and any folder in STATICFILES_DIR
. If you have project-wide static resources, you need to create an entirely separate folder, different than both STATIC_ROOT
and MEDIA_ROOT
and add that to STATICFILES_DIRS
. I usually call it "assets", but it's name doesn't matter.
Upvotes: 4
Reputation: 174614
media
and static
to STATICFILES_DIRS
in settings.pyTEMPLATE_CONTEXT_PROCESSORS
in settings.py has 'django.core.context_processors.static',
in there. Its there by default.{{ STATIC_URL }}images/ball.png
in your templates.Upvotes: 1