Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to serve static files like css, js in django python

I am very new to django, and gone through tutorial for many days , i have started building a small website using django and trying to serve a css file by arranging all the necessary settings in settings.py file. But unfortunately my code is unable to serve the css file, i mean the concept of serving css files is not working. I googled a lot and gone through the django main doc tutorials and made changes according to them,and still doesn't works so approached SO and pasted my entire code below

Structure of project folder

 personnel_blog
      |____personnel_blog
      |____manage.py  |  
                      |____media
                      |____static
                      |       |____css
                      |             |____personnel_blog_hm.css 
                      |____template 
                      |        |____home_page.html
                      |____settings.py
                      |____urls.py
                      |____views.py
                      |____wsgi.py         

Some of my settings.py file settings are below

settings.py

import os
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
                    os.path.join(PROJECT_DIR,'static'),
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)
TEMPLATE_DIRS = (
                 os.path.join(PROJECT_DIR,'templates')
)
TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
)

urls.py

from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
     url(r'^$', 'personnel_blog.views.home_page'),
     url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True }),
        url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True }),

)

views.py

from django.shortcuts import render_to_response

def home_page(request):
    return render_to_response("home_page.html")

home_page.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
  <head>
    <link rel="stylesheet" href="{{ STATIC_URL }}css/personnel_blog_hm.css" type="text/css">
  </head>
  <body>
   <p>Hello !</p>
   <a href="/" target="_top">Home</a>
  </body> 
</html>  

personnel_blog_hm.css

body { background-color:green; }
p {color:blue;background-color:green;padding-left:20px;}

So above is my code, can anyone please let me know whats wrong in the settigns.py file or other py files ?

Whether need to do any other additional settings in the above code ?

so can anyone please adjust my code and make necessary changes so that i cam move forward and make my first step in web designing ..... :)

like image 545
Shiva Krishna Bavandla Avatar asked Feb 26 '13 05:02

Shiva Krishna Bavandla


People also ask

Can Django serve static files?

Django does not support serving static files in production. However, the fantastic WhiteNoise project can integrate into your Django application, and was designed with exactly this purpose in mind. See the WhiteNoise Django documentation for more details.

How can you set up static files in Django?

Configuring static filesMake sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.

How do you serve static files in Django in production?

Serving static files in production. The basic outline of putting static files into production consists of two steps: run the collectstatic command when static files change, then arrange for the collected static files directory ( STATIC_ROOT ) to be moved to the static file server and served.


2 Answers

base.html

{% load static %}

<link rel="stylesheet" href="{% static 'css/personnel_blog_hm.css' %}" type="text/css">

settings

PROJECT_DIR  = os.path.dirname(__file__) 

MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_DIR, 'staticfiles'),
)

url

from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
     url(r'^$', 'personnel_blog.views.home_page'),
     url(r'^admin/', include(admin.site.urls)),
)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()
like image 73
catherine Avatar answered Oct 25 '22 23:10

catherine


Try changing your STATICFILES_DIRS setting to

STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR,'static'),
)
like image 24
arulmr Avatar answered Oct 25 '22 22:10

arulmr