I have image in my applications directory. I want to use this image in my template. I use Ajax request for getting path of image and i want to show image in template. How i can do it? My code:
def posts(request):
if request.is_ajax():
offset = request.GET.get('offset')
limit = request.GET.get('limit')
all_posts = Post.objects.all().order_by('-pub_date')[offset:limit]
if all_posts:
data = []
for obj in all_posts:
# image = PostImage.objects.filter(pk=obj.pk)
image = obj.postimage_set.all()
js = {
'info': 'success',
'id': obj.pk,
'title': obj.title,
'description': obj.description,
'pub_date': obj.pub_date.strftime("%d.%m.%Y %H:%M"),
}
if image:
img = {'image': str(image[0].image)}
else:
img = {'image': ''}
js.update(img)
data.append(js)
return HttpResponse(json.dumps(data), content_type='application/json; charset=utf8')
else:
return HttpResponse(404)
else:
return render(request, 'main.html')