Question

I am splitting my hair over this one. Have gone through some of the existing Q&A on StackOverflow, but it hasn't helped resolve my situation. In fact I remember sometime back during previous iteration of my development, {{ MEDIA_URL }} used to work fine. But now it is suddenly blank.

Here is are the details of my environemnt/settings (providing relevant snippets of code):

In settings.py I have defined the following

MEDIA_URL = '/media/'
MEDIA_ROOT = '/path/to/dir'

In urls.py:

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

In template file:

<img src={{ MEDIA_URL }}{{ employee.photo }} alt="photo" align=left valign=top/>

In views.py:

class EmployeeView(ListView):
    model = Employee
    template_name = 'CompanyApp/displayEmployees.html'
    context_object_name = 'EmployeeList'

In other related questions, I read people talk about two kind of solutions (neither of them seem to work for me). More details below:

Solution # 1 proposed is to add the following to TEMPLATE_CONTEXT_PROCESSORS:

'django.core.context_processors.media'

But when I do this, I get the following error on my dev environment:

[12/May/2014 23:37:34] "GET / HTTP/1.1" 500 59
Traceback (most recent call last):
  File "/usr/lib/python2.6/wsgiref/handlers.py", line 93, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/lib/python2.6/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__
    return self.application(environ, start_response)
  File "/usr/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 187, in   __call__
    self.load_middleware()
  File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 49, in lo  ad_middleware
    mw_instance = mw_class()
    TypeError: media() takes exactly 1 argument (0 given)

Solution #2 I see being proposed is to return RequestContext in the views. But as one could see above, I am using ListView and it is automatically rendered to the template.

Any expert insight into how to make django recognize the {{ MEDIA_URL }} value again would be much appreciated. Thanks.

P.S. BTW how do I post code? It says in the instructions that I have to prefix it by four spaces. But it was a pain for me to use the space bar to prefix each line of code by four spaces, one line at a time. Is there a easier way to accomplish this?

Était-ce utile?

La solution

According to what you said, the error is the same related here:

https://groups.google.com/forum/#!topic/django-brasil/f-5WwMA2bac the context processor is listed in MIDDLEWARE_CLASSES instead of TEMPLATE_CONTEXT_PROCESSORS.

Autres conseils

try add context_instance=RequestContext(request) in your view

def something(request):
   #your view goes here
   render_to_response('template.html',{#your_dict_items},context_instance=RequestContext(request))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top