在 Django 中注入 js 变量 url
Injecting js variable in django url
我的 url 看起来像这样:
path('tree_sensor\/(?P<sensorID>.*)', views.tree_sensor, name='tree_sensor')
我有一个 var sID = 1,2,etc
,我想将其注入按钮中的 django url
<a class='popup_button small_text' href='{% url 'agriculture:tree_sensor' "sID" %}'><span>Sensor Page</span></a>")
模板:
for (i in motePos) {
markers.addLayer(mote[i].on('click', function(e) {
var sID = this.id;
$.ajax({
type : 'GET',
url : "{% url 'agriculture:sensor_ms' %}",
data : {"sID" : sID},
success: function(response){
var measurement = JSON.parse(response["measurement"]);
var fields = measurement[0]["fields"];
console.log(sID)
mote[i].bindPopup("<h3>Tree Sensor: " + motePosName[i] + "</h3>"
"<br><a class='popup_button small_text' href='{% url 'agriculture:tree_sensor' sID %}'><span>Sensor Page</span></a>").openPopup();
mote[i].name = motePosName[i];
mote[i].id = motePosId[i];
}
})
}));
当前 url 打印“tree_sensor/sID”。我想从对象中获取 ID,所以它类似于“tree_sensor/1”“tree_sensor/2”等
正如 DrummerMann 所建议的那样:
var sensorID = "href='{% url 'agriculture:tree_sensor' 12345 %}'";
var url_mask = sensorID.replace("12345", fields["sensor"]);
和 link
的按钮
"<br><a class='popup_button small_text' "+url_mask+" ><span>Sensor Page</span></a>"
我的 url 看起来像这样:
path('tree_sensor\/(?P<sensorID>.*)', views.tree_sensor, name='tree_sensor')
我有一个 var sID = 1,2,etc
,我想将其注入按钮中的 django url
<a class='popup_button small_text' href='{% url 'agriculture:tree_sensor' "sID" %}'><span>Sensor Page</span></a>")
模板:
for (i in motePos) {
markers.addLayer(mote[i].on('click', function(e) {
var sID = this.id;
$.ajax({
type : 'GET',
url : "{% url 'agriculture:sensor_ms' %}",
data : {"sID" : sID},
success: function(response){
var measurement = JSON.parse(response["measurement"]);
var fields = measurement[0]["fields"];
console.log(sID)
mote[i].bindPopup("<h3>Tree Sensor: " + motePosName[i] + "</h3>"
"<br><a class='popup_button small_text' href='{% url 'agriculture:tree_sensor' sID %}'><span>Sensor Page</span></a>").openPopup();
mote[i].name = motePosName[i];
mote[i].id = motePosId[i];
}
})
}));
当前 url 打印“tree_sensor/sID”。我想从对象中获取 ID,所以它类似于“tree_sensor/1”“tree_sensor/2”等
正如 DrummerMann 所建议的那样:
var sensorID = "href='{% url 'agriculture:tree_sensor' 12345 %}'";
var url_mask = sensorID.replace("12345", fields["sensor"]);
和 link
的按钮"<br><a class='popup_button small_text' "+url_mask+" ><span>Sensor Page</span></a>"