2014-01-22 88 views
0

我有一個頁面,用戶可以搜索其他用戶。搜索是通過ajax完成的,結果通過模板循環播放,然後放到頁面上。搜索完美無缺,但MEDIA_URL在結果中顯示爲空,並打破結果的所有個人資料圖片。 MEDIA_URL標記可在網站的其他任何地方使用,只是不在此模板中。我爲我的模板使用pyjade。django media_url with ajax

蟒蛇在搜索結束

#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 }} 
+2

我不知道這個因此評論,但你可以嘗試通過render_to_string一個的RequestContext實例,即:'從django.template import RequestContext; r = render_to_string('users/search_ajax_template.jade',{'results':p},RequestContext(request))' –

回答

1

需要MEDIA_URL添加到語境詞典渲染返回結果。

選項1:

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

選項2:

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