开发者

JQuery post not passing variable

开发者 https://www.devze.com 2023-01-22 03:38 出处:网络
I\'m trying to pass an image\'s src to a django view when a button is clicked.In my template, I have:

I'm trying to pass an image's src to a django view when a button is clicked. In my template, I have:

$("#url_submit").click(function() {
   var film = "{{ filmname }}"
   var id = {{ id }}
   $.ajax({
     url: "/db/gallery2/" + film + "/" + id + "/",
     data: {url: $('#large_thumbnail').attr('src')}开发者_开发技巧,
     type: "POST"
   });  
});

My view reads:

def thumbnail_choice(request, filmname, id):
    if request.is_ajax:
         if request.method == "POST":
            url = request.POST['url']
            if url != "":
                 mdlnm = get_model('db', filmname.lower())
                 object = get_object_or_404(mdlnm, id__iexact=id)
                 object.url_small = url
                 object.save()
                 return HttpResponseRedirect("/db/")
    return render_to_response('gallery2.html', {'filmname': filmname, 'id': id})

When I submit the page though, I get an error:

"Key 'url' not found in <QueryDict: {}>"

I'm sure there are a combination of things I'm doing wrong here.


D'Oh. Sorry for wasting your time. Rookie mistake:

if request.is_ajax():

NOT

if request.is_ajax:

Thanks for the comments though

0

精彩评论

暂无评论...
验证码 换一张
取 消