I have a problem loading up CSS from the static directory:
settings.py
MEDIA_ROOT = '/he/sites/video1.hackedexistence.com/htdocs/media/'
MEDIA_URL = '/media/'
STATIC_ROOT = '/he/sites/video1.hackedexistence.com/htdocs/static/'
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
STATICFILES_DIRS = ()
url.py
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
r'^beers/$', 'beer.views.BeersAll'),
)
base.html loads up fine, css won’t load
<link rel="stylesheet" type="text/css" href="/static/css/video1.css" />
the css href link leads to
Page not found (404)
'css/video1.css' could not be found
The full path to the css file:
/he/sites/video1.hackedexistence.com/htdocs/static/css/video1.css
I’m following a youtube django tutorial by Hacked Existence. and he seems to proceed without a problem with this setup. How can i correct this?
Hacked Existence probably has his web server (apache based on your last question) configured to serve his static files.
user1658078 is correct in that you need to serve the static files in some way, and in a development environment you can use django’s built-in view
django.contrib.staticfiles.views.serve(request, path)– all this view does is it looks at the path set inSTATICFILES_DIRSand thestaticsubdirectory inside each application (e.g. if your project is my calledmysiteand it has an application calledblog, then it will look inmysite/blog/static/), and serves any files which match the portion of the URL after the value ofsettings.STATIC_URL.Finally, it’s worth pointing out that your settings won’t work at all at the moment, because your MEDIA_ROOT and STATIC_ROOT directories are set to serve from subdirectories of
/he/sites/video1.hackedexistence.com/htdocs/, and unless you’ve created this directory, it won’t work at all.To fix static files, templates, admin files & uploaded files, follow these instructions:
In your
settings.py, replace the lines in your question with the following:Also make sure
DEBUGis set toTrue.In your urls.py inside your django project (i.e. not inside your app directory of
beer), add the following at the end:Create the following directories inside your django project directory:
/media/– this is where files uploaded by users go (e.g. via FileField or ImageField)/static/– this is where you can put static files which apply to your whole django project (e.g. your styles for the whole page). For example, with this configuration, if you are trying to access the css filevideo1.cssat the urlstatic/css/video1.css, then you make sure the file is at the following path:/static/css/video1.css/templates/– this is where you put templates which apply to your whole django project/beer/static/– this is where you put static files which apply only to one specific site. When build urls to files in here, you treat them just as if they’re in the/static/directory, so just prepend the value ofSTATIC_URLto the relative filename./beer/templates/– when you need to start creating templates for your views, you put your templates in here (theTEMPLATE_LOADERSsetting includesdjango.template.loaders.app_directories.Loaderby default, which will find templates in this directory). Similar to the application specific static directory, just treat files in this directory as if they are in the normal/templates/directory./static-serve/– this isn’t used during development (so don’t worry too much for now), but when you eventually want to deploy your django application, you run ./manage.py collectstatic, which will cause django to copy all files from each application directory’sstaticdirectory and put them in here, and then you configure your web server to serve your files from this directory when the url starts with the value ofSTATIC_URL(/static/in this case).Now your static files will be loaded properly, the admin will display properly, your user uploaded files will be able to be served properly, and your templates will be found properly when you need them.