Using static files with django virtual server

Questions like this were asked before, I read them all and tried to understand django's official documentation on this, but I'm still trying to use static files on a virtual server. More specifically, I'm just trying to get my Base.html template to use base.css.

My folder structure is as follows:

 manage.py static/CSS/base.css VergeGreenITEvent_Website/settings.py VergeGreenITEvent_Website/views.py ect VergeGreenITEvent_Website/Webpage_Templates/Base.html 

(There is currently no application folder, as I followed the “django Book” to learn and did not get to it!)

Full settings.py can be viewed here: http://pastebin.com/JB3mKRcJ

In my Base.html template, I now have the code:

 <head> <title>Verge Green IT</title> {% load static %} <link rel="stylesheet" href="{% static "CSS/base.css" %}" type="text/css" /> </head> 

CSS is still not applied. enter image description here Could you help me figure out what I'm doing wrong? I would be incredibly grateful.

I am using the latest version of Django. (1.4)


urls.py:

 from django.conf.urls import patterns, include, url from django.conf import settings from django.contrib.staticfiles.urls import staticfiles_urlpatterns import views urlpatterns = patterns('', url(r'^$', views.Home), ) if settings.DEBUG: urlpatterns += staticfiles_urlpatterns() #this serves static files and media files. #in case media is not served correctly urlpatterns += patterns('', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.MEDIA_ROOT, }), ) 
+3
source share
2 answers

If you are using Django 1.4, I would use a static tag:

 {% load static %} <link rel="stylesheet" href="{% static "CSS/base.css" %}" type="text/css" /> 

Maybe you need it too in your urls, in development files they are sent via django

 if settings.DEBUG: urlpatterns += staticfiles_urlpatterns() #this serves static files and media files. #in case media is not served correctly urlpatterns += patterns('', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.MEDIA_ROOT, }), ) 

It’s also common practice in your settings to avoid hard-coded locations, here is an example (by the way, do you have static file sensors in your settings?

 PROJECT_ROOT = path.dirname(path.abspath(__file__)) #gets directory settings is in STATIC_ROOT = path.join(PROJECT_ROOT,'static-root') # this folder is used to collect static files in production. not used in development STATIC_URL = "/static/" STATICFILES_DIRS = ( ('', path.join(PROJECT_ROOT,'static')), #store site-specific media here. ) # List of finder classes that know how to find static files in # various locations. STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', ) TEMPLATE_CONTEXT_PROCESSORS = ( # other processors... "django.core.context_processors.static", ) 
+3
source

You should write your settings.py as follows:

 import os PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) STATIC_URL = '/static/' # Should be root relative or absolute STATICFILES_DIRS = ( os.path.join(PROJECT_ROOT, 'static'), ) # Where your static files will be collected with colletstatic command # I use it only in deployed configuration with DEBUG=False # Need to be outside STATICFILES_DIR STATIC_ROOT = '/var/www/your.site' 

To be sure, check with Firebug or the webkit debugger that CSS is loaded.

Remember that Django will not serve your static files if settings.DEBUG == False .

0
source

All Articles