thchand
thchand

Reputation: 368

Ajax request from django template, url and view

I have following settings

URL

urlpatterns = patterns('',
   (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_DOC_ROOT}),
    url(r'^index/$','pMass.views.index', name='index'),
    url(r'^index/(?P<match>\d+)/(?P<tab>\d)$', 'pMass.views.detail',name='detail'),

VIEW

def index(request):
    error = False
    cid = request.GET

    if 'cnum' in request.GET:
       cid = request.GET['cnum']

    if not cid:
       error = False
       expcount = Experiment.objects.count()
       allmass = SelectedIon.objects.count()

    else:

        defmass = 0.000001
        massvalue = float(cid)
        masscon = defmass * massvalue
        highrange = massvalue + masscon
        lowrange = massvalue - masscon

        myquery = SelectedIon.objects.select_related().filter(monoiso__range=(lowrange, highrange))
        querycount = myquery.count()

        return render_to_response('queryresult1.html', {'query': cid, 'high':highrange, 'low':lowrange, 'sections':myquery, 'qcount':querycount, })


    return render_to_response('index.html', {'error': error, 'exp': expcount,'mass':allmass,})


def detail(request,  match, tab):
    monorecord = get_object_or_404(SelectedIon, monoiso=match)
    detailrec = SelectedIon.objects.filter(monoiso=monorecord)
    return render_to_response('queryresult1.html', {"id": monorecord, "detail": detailrec}, context_instance=RequestContext(request))

Template // from where I am trying to send request

$("td a").bind("click", function(event){

        var str = $(this).attr('id');
        tab = $("ul.tabs li").find("a").attr('id');
        mapurl = 'match/'+ str+ '/tab/'+ tab;

        new $.ajax({
        url: mapurl,
        async: true,
        // The function below will be reached when the request has completed
        success: function(transport)
        {
            $("#result").html(transport); // Put data in the div
            $("result").fadeIn();        // Fade in the active content
        }
    });

I am trying to send ajax request to server and get result back in the same queryresult1.html template (result containter). But, I am getting problem in my request

`Error`     [11:18:37.814] GET http://127.0.0.1:8000/index/match/622/tab/1 [HTTP/1.0 404 NOT FOUND 59ms]

I think my url configuraton is right? How can I solve my ajax request from template with it's corresponding url and view?

Upvotes: 0

Views: 2195

Answers (2)

disorder
disorder

Reputation: 291

I am not sure but I think you should try: http://127.0.0.1:8000/index/622/1 to match given url pattern.

Consider building your urls by using {% url (...) %} tag

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599580

This has nothing to do with Ajax.

You are requesting the URL index/match/622/tab/1. But your URLconf is expecting index/622/1 - no 'match' or 'tab'.

Upvotes: 2

Related Questions