Sunday, 30 October 2011

How to Use Css or Static Files in django Python

It is important to use static files in web application. one common item is Cascading style sheets (.css). Normarlly if you put css file even in where you placed template file but relative path to link css doesn't works. Because in django absolute path is required but its not a common practice. inorder to use static files you need to put them in folder. folder is better to be placed in root directory in your project. then make Modification to Settings.py & urls.py to Make that Folder accessable on domain to use them in your project.

e.g. if you have Named Static files folder myFiles then you can refer css file from your html Tag by /myFiles/style.css

you are required to make these changes:

  • In "settings.py", import "os.path"
import os.path
  • Change "MEDIA_ROOT", "MEDIA_URL" and "ADMIN_MEDIA_PREFIX" to
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), "MyFiles")
MEDIA_URL = '/MyFiles/'
ADMIN_MEDIA_PREFIX = '/MyFiles/admin/' (anything other than media)
  • In "urls.py", include the following imports
from django.views.static import *
from django.conf import settings
  • And the following URL handler
# Required to make static serving work
(r'^MyFiles/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
Restart your server and it should be accessible from http://www.domain.com/MyFiles/style.css

Auto Settings Script

Here is Script copy and paste it to file and save file with static.py extention. Now copy that file in root of your django project where settings.py file is placed. Now run this Script

Script:
folder = raw_input("Enter Name for Folder in your Project Root! :> ")
contents = ""
for line in open("Settings.py", "r"):
    if(line.find("MEDIA_ROOT")>-1):
        contents = contents + "MEDIA_ROOT = os.path.join(os.path.dirname(__file__), \"" + folder + "\")\n"
    elif(line.find("MEDIA_URL")>-1):
        contents = contents + "MEDIA_URL = \'/" + folder + "/\'\n"
    elif(line.find("ADMIN_MEDIA_PREFIX")>-1):
        contents = contents + "ADMIN_MEDIA_PREFIX = \'/" + folder + "/admin/\'\n"
    else:
        contents = contents + line
if(contents.find("import os.path")==-1):
    contents = "import os.path\n" + contents
f=open("Settings.py","w")
f.write(contents)
#print contents
contents = ""
for line in open("urls.py", "r"):
    if(line.find("django.views.static.serve")>-1):
        continue
    contents = contents + line
    if(line.find("urlpatterns")>-1):
        contents = contents + "    (r'^" + folder + "/(?P<path>.*)$', 'django.views.static.serve', {\'document_root\': settings.MEDIA_ROOT}),\n"
   
#print contents
        if(contents.find("django.views.static import")==-1):
            contents = "from django.views.static import *\n" + contents
        if(contents.find("from django.conf import settings")==-1):
            contents = "from django.conf import settings\n" + contents
           
f=open("urls.py","w")
f.write(contents)
print "Now Create Folder Named " + folder + " on Root Dir and keep files you want there.\ne.g. if you have placed file t.css in sub Dir css\nThen you could find it at http:\\yourdomain.com\\"+folder+"\css\\t.css"
raw_input()