?Briella 2014-01-22 01:08 采纳率: 0%
浏览 43

带有ajax的django media_url

I have a page where users can search for other users. The search is done with ajax and the results are looped through in a template and then placed onto the page. The search works perfectly, but MEDIA_URL is showing up empty in the results and breaks all the profile pictures for the results. The MEDIA_URL tag works everywhere else on the site, just not in this template. I am using pyjade for my templates.

python to return results at end of search

#render template to string, pass to js to add to page
r = render_to_string('users/search_ajax_template.jade', { 'results': p })
return HttpResponse(json.dumps({'results': r, 'errcode': 0}))

search_ajax_template.jade

- for result in results
    div.result-wrapper
        a(href='/profiles/{{ result.path }}', class='result')
            img(src='{{ MEDIA_URL }}{{ result.prof_pic }}')
            div.result-name {{ result.name }}
  • 写回答

1条回答 默认 最新

  • helloxielan 2014-01-22 03:29
    关注

    Need to add MEDIA_URL to the context dictionary for rendering.

    Option 1:

    from django.template import RequestContext
    ...
    ...
    ...
    r = render_to_string('users/search_ajax_template.jade', { 'results': p }, context_instance=RequestContext(request))
    

    Option 2:

    from settings import MEDIA_URL
    ...
    ...
    ...
    r = render_to_string('users/search_ajax_template.jade', { 'results': p, 'MEDIA_URL': MEDIA_URL })
    
    评论

报告相同问题?