Django url rewrites and passing a parameter from Javascript

Posted by William T Wild on Stack Overflow See other posts from Stack Overflow or by William T Wild
Published on 2011-08-23T20:34:51Z Indexed on 2012/11/02 17:03 UTC
Read the original article Hit count: 144

Filed under:
|

As a bit of a followup question to my previous , I need to pass a parameter to a view. This parameter is not known until the JS executes.

In my URLConf:

url(r'^person/device/program/oneday/(?P<meter_id>\d+)/(?P<day_of_the_week>\w+)/$',
therm_control.Get_One_Day_Of_Current_Thermostat_Schedule.as_view(), 
name="one-day-url"),

I can pass it this URL and it works great! ( thanks to you guys).

http://127.0.0.1:8000/personview/person/device/program/oneday/149778/Monday/

In My template I have this:

var one_day_url = "{% url personview:one-day-url meter_id=meter_id day_of_the_week='Monday' %}";

In my javascript:

 $.ajax({
        type: 'GET',
        url: one_day_url ,
        dataType: "json",
        timeout: 30000,
        beforeSend: beforeSendCallback,
        success: successCallback,
        error: errorCallback,
        complete: completeCallback
    });

When this triggers it works fine except I dont necessarily want Monday all the time.

If I change the javascript to this:

var one_day_url = "{% url personview:one-day-url meter_id=meter_id %}";

and then

$.ajax({
        type: 'GET',
        url: one_day_url + '/Monday/',
        dataType: "json",
        timeout: 30000,
        beforeSend: beforeSendCallback,
        success: successCallback,
        error: errorCallback,
        complete: completeCallback
    });

I get the Caught NoReverseMatch while rendering error. I assume because the URLconf still wants to rewrite to include the ?P\w+) .

I seems like if I change the URL conf that breaks the abailty to find the view , and if I do what I do above it gives me the NoREverseMatch error.

Any guidance would be appreciated.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about django