【发布时间】:2015-04-04 06:35:33
【问题描述】:
我正在尝试在我的模板标签中使用 {{ MEDIA_URL }}。这是我的html代码:
<li data-ng-repeat="image in pictures[currentCat]">
<a>
<img src="{{ MEDIA_URL }}{$ image.imgs $}" />
</a>
</li>
我将 json 发送到一个 js 并在 AngularJS 中使用它
$http.get("imagelist/").success(function(data) {
$scope.pictures=data;
});
我已将此部分添加到我的setting.py:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.media",
)
现在this answer 建议这样做
from django.template.context import RequestContext context = {'latest': p} render_to_response('Question/latest.html', context_instance=RequestContext(request, context))
但我使用的是HttpResponse,但它不适用于我的代码。
def getimagelist(request):
dictionaries=[]
for cat in Category.objects.all():
dictionary=[obj.as_json() for obj in Gallery.objects.filter(cat_id=cat.id)]
dictionaries.append(dictionary)
return HttpResponse(json.dumps(dictionaries), content_type='application/json')
我的问题是在使用HttpResponse 时如何在我的代码中使用{{ MEDIA_URL }}?
目前它在我的 html 中显示为空白。 谢谢
【问题讨论】:
-
您想在哪里使用 MEDIA_URL?您的响应对象没有对您的模板的任何引用,在这种情况下,您将获得的唯一响应将是您的字典转储。
-
这个问题没有意义。您根本没有使用任何模板。你把那个 STATIC_URL 标签放在哪里了?
-
@DanielRoseman 它的
<img src="{{ MEDIA_URL }}{$ image.imgs $}" />我编辑了我的问题 -
但这仍然没有意义。您的观点根本没有提及任何模板。那么该模板标签在哪里使用呢?
-
@DanielRoseman 我用更多细节更新了我的问题