作为我之前的一个后续问题,我需要将参数传递给视图.在JS执行之前,不知道该参数.
在我的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"),
Run Code Online (Sandbox Code Playgroud)
我可以传递这个URL,它很棒!(谢谢你们).
http://127.0.0.1:8000/personview/person/device/program/oneday/149778/Monday/
Run Code Online (Sandbox Code Playgroud)
在我的模板中,我有这个:
var one_day_url = "{% url personview:one-day-url meter_id=meter_id day_of_the_week='Monday' %}";
Run Code Online (Sandbox Code Playgroud)
在我的javascript中:
$.ajax({
type: 'GET',
url: one_day_url ,
dataType: "json",
timeout: 30000,
beforeSend: beforeSendCallback,
success: successCallback,
error: errorCallback,
complete: completeCallback
});
Run Code Online (Sandbox Code Playgroud)
当这触发它工作正常,除了我不一定想要周一.
如果我将javascript更改为:
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
});
Run Code Online (Sandbox Code Playgroud)
我在渲染错误时得到了Caught NoReverseMatch.我假设因为URLconf仍然想要重写以包含?P\w +).
我好像如果我改变了破坏abailty的URL conf来查找视图,如果我做我上面做的事情,它会给我NoREverseMatch错误.
任何指导将不胜感激.
我通常会做一些事情
var one_day_url = "{% url personview:one-day-url meter_id=meter_id day_of_the_week='REPLACE_ME' %}";
// ...
url: one_day_url.replace('REPLACE_ME', 'Sunday')
Run Code Online (Sandbox Code Playgroud)