In my django app ,I have put these settings
settings.py
------------
MEDIA_ROOT = '/home/me/dev/python/django/myproject/myapp/media/'
MEDIA_URL = '/site_media/'
urls.py
--------
...
url(r'^myapp/', include('myapp.urls')),
url(r'^site_media/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.MEDIA_ROOT}),
I created a css file in '/home/me/dev/python/django/myproject/myapp/media/css' folder and included this in template as below
base.html
--------
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>my app</title>
<LINK REL=StyleSheet HREF="{{MEDIA_URL}}css/myappstyle.css" TYPE="text/css" MEDIA="screen, print"/>
home.html
---------
{% extends "mywebapp/base.html" %}>
{% block title %}{{block.super}}| Home {% endblock %}
{% block content %}
{{block.super}}
hi
{% endblock %}
Then I created a view as below
def custom_render(request,context,template):
req_context=RequestContext(request,context)
return render_to_response(template,req_context)
def home(request,template_name,page_title):
print 'home'
return render_to_response(template_name,{'me':'myname'})
and set its url conf
myapp.urls
--------
urlpatterns=patterns('',
url(r'^$','myapp.views.home',
{
'template_name':'mywebapp/home.html',
'page_title':'Home'
},
name='home'),
when I give the url
http://127.0.0.1:8000/myapp/
The web page gets rendered ,but css is not.The console output shows a 404 error
[14/Sep/2012 10:02:50] "GET /myapp/ HTTP/1.1" 200 547
[14/Sep/2012 10:02:50] "GET /myapp/css/myappstyle.css HTTP/1.1" 404 2514
But,when I give the url,
http://127.0.0.1:8000/site_media/css/myappstyle.css
I can see the contents of the css file without any problems.
Why does this happen? can someone help me figure this out?
You need to use
RequestContext()while rendering response forhomeview also (as you have done forcustom_renderview).Otherwise
{{MEDIA_URL}}will be empty which will result in404error.