0

I am developing a WebApp using Django.I m a newbie, I have this html file which contains image, i did go through this "link", but now i am getting an error as in parsing the Media Url. Could any let me know what is wrong? I have checked the permissions, even they are correct. !!

This is that image which i am including -

<img border="2" src="{{ MEDIA_URL }}samplelogo.jpg" alt="logo here"/>

This is the urls.py -

from django.conf.urls.defaults import patterns, include, url
import settings

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^gharnivas/$', 'gharnivas.views.index'),
    url(r'^media/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
)

This is the settings.py with media url and root :

MEDIA_ROOT = '/home/nagaraj/ghar/templates/media'
MEDIA_URL = 'http://localhost:8000/media/'

ghar is the django project and gharnivas is the app

Update :

Now I have removed the error of TemplateSyntaxError by replacing Media url to '/media/' as suggested by @Torsten below. But still not getting the image !!

Another query , is it correct that i am using MEDIA_ROOT And MEDIA_URL ? Because some place i even see that they have STATIC_URL and STATIC_ROOT !!

Community
  • 1
  • 1
Nagaraj Tantri
  • 5,172
  • 12
  • 54
  • 78
  • I don't know if you're missing something, but we definitely are, because you haven't posted the template where you reference this. – Daniel Roseman Apr 27 '11 at 09:16

2 Answers2

1

MEDIA_URL = '/media/' instead of MEDIA_URL = 'http://localhost:8000/media/'

Torsten Engelbrecht
  • 13,318
  • 4
  • 46
  • 48
  • I did change it, but still no progress. The fact is i had first written "http://localhost:8000/media/" because there was an example which was commented saying #Examples: "http://media.lawrence.com/media/", "http://example.com/media/" – Nagaraj Tantri Apr 27 '11 at 09:28
  • My appologies, I had a small semicolon along with '/media/' . But even though the TemplateSyntaxError is removed. My media is still not been seen. I mean, i still dont get a image – Nagaraj Tantri Apr 27 '11 at 09:40
  • 1
    As I can see your problem was somewhere else. Anyway, you'll never know how your project will change with time. If domain of you site is changing you are on the safe side just using `'/media/'`. – Torsten Engelbrecht Apr 28 '11 at 08:10
1

{{ MEDIA_URL }} outputs empty string? Make sure that

1). 'django.core.context_processors.media' is in TEMPLATE_CONTEXT_PROCESSORS

2). You pass RequestContext in your template (by passing context_instance=RequestContext(request) in render_to_response or by using direct_to_template instead).

From Django 1.8 'django.core.context_processors.media' was moved to 'django.template.context_processors.media'

DrTyrsa
  • 31,014
  • 7
  • 86
  • 86
  • @DrTyrsa : I m confused towards your explanation. As i am a newbie with Django, I dint get your 1st point as to where can i add it? Even with my views.py i have got render_to_response('gharnivas/ghar.html') which loads the first file but without the image . – Nagaraj Tantri Apr 27 '11 at 12:07
  • `TEMPLATE_CONTEXT_PROCESSORS` is in project's settigs.py. `'django.core.context_processors.media'` should be there by default. This processor adds MEDIA_URL variable to your template. But you should use `RequestContext`. [Here](http://stackoverflow.com/questions/3756841/django-media-url-blank)'s the related question. – DrTyrsa Apr 27 '11 at 12:12
  • @DrTyrsa : I am sorry :(, not been able to find it in settings.py. I checked it throughly :| – Nagaraj Tantri Apr 27 '11 at 12:15
  • That's strange, but if it isn't there you can place it there. Here: http://docs.djangoproject.com/en/1.2/ref/settings/#template-context-processors you can find how it looks by default. – DrTyrsa Apr 27 '11 at 12:20
  • Should i add it by myself? I mean i didn't find any inbuilt TEMPLATE_CONTEXT_PROCESSORS :| – Nagaraj Tantri Apr 27 '11 at 12:20
  • Could u give ur mail id? because i will just mail u my entire settings.py file . As its freaking me out when i am not able to find it on my project folder :| – Nagaraj Tantri Apr 27 '11 at 12:22