jquery - Ajax download image in template -
i have image in applications directory. want use image in template. use ajax request getting path of image , want show image in template. how can it? 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 = [] 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')
and what's problem code? , what's problem action?..
to import image in template ajax, need to:
- make ajax request view function (here : posts)
- get path of image (so apparently you're ok that)
- return path template return of function
- display image can see in this question on stackoverflow
example:
$('#list li a').click(function () { var url = $(this).attr('href'), //replace ajax call function image = new image(); image.src = url; image.onload = function () { $('#image-holder').empty().append(image); //replace id }; image.onerror = function () { $('#image-holder').empty().html('that image not available.'); //replace id } $('#image-holder').empty().html('loading...'); //replace id return false; }); </pre>
Comments
Post a Comment