我有一个 Django 应用程序,我试图将其中一个页面中的一些复选框重定向到在我的数据库中执行某些操作的视图(切换与复选框所代表的信息相对应的布尔列)。我在 javascript redirects 上找到了这个页面如何在 JavaScript/jQuery 中重定向到另一个网页?但这些建议都没有做任何事情。这是我的相关代码。
我的模板
<script>
function toggleCompletion(task_id){
window.location.replace("/maintenance/tasks/{{ maintenance.id }}/toggleCompletion/" str(task_id) + "/";
}
</script>
...
...
<input type="checkbox" onclick="toggleCompletion({{ task.id }});" />
我的网址配置
urlpatterns = patterns('MaintenanceTracker.views',
url(r'^tasks/(?P<main_id>\d+)/toggleCompletion/(?P<task_id>\d+)/', 'toggleCompletion'),
模板尝试重定向到的视图现在实际上是空的(在 HttpResponse 中只有一个带有测试字符串的返回行)。单击有问题的复选框会导致复选框更改状态,我确认它正在进入带有警报的 JS,但没有进一步发生。我还尝试根据我找到的页面的建议修改 windows.location.href 和 location.href 来解决这个问题。提前感谢您的帮助,如果您需要更多信息,请询问,我会提供。
编辑:根据 abarnert 的要求,这里是单击复选框之前和之后的页面源。
前:
<head>
<title>Task List</title>
<style>
table {
margin-left: 100;
margin-top: 15;
}
h1 {
margin-left: 100;
margin-top: 75;
}
h3 {
margin-left: 100;
margin-top: 50;
}
.indent {
margin-left: 100;
}
.navigator {
position:absolute;
right:25px;
top:15px;
}
.navigator-panel {
position:absolute;
right:25px;
top:60px;
}
.user-info {
position:absolute;
left:25px;
top:15px;
}
.logout {
position:absolute;
left:25px;
top:60px;
}
</style>
<script>
//redirects to toggleCompletion view to toggle the value of one of the tasks isDone field
function toggleCompletion(task_id){
alert(task_id);
location.href = "/maintenance/tasks/2/toggleCompletion/" + str(task_id) + "/";
//window.location.replace("/maintenance/tasks/2/toggleCompletion/" + str(task_id) + "/");
}
</script>
<h1>Task List for July 2013 Maintenance</h1>
</head>
<!--Create a User info panel-->
<p class="user-info">You're logged in as jgreen</p>
<form class="logout" method="post" action="/maintenance/logoutResolve/">
<input type='hidden' name='csrfmiddlewaretoken' value='3vJTLWV0aIqAYfPIX2toBSPPFTVnE7Ll' />
<input type="submit" value="Logout" />
</form>
<!--Display table of the Maintenances-->
<body>
<table border="2">
<tr>
<th>Time</th>
<th>Task Description</th>
<th>Owner</th>
<th>Notes</th>
<th>Done?</th>
</tr>
<tr>
<td>July 16, 2013, 10:25 a.m.</td>
<td>Add a Task</td>
<td>jgreen</td>
<td>adlfjalkds</td>
<td><input id="1Checkbox" type="checkbox" value="False" onclick="toggleCompletion(1);" /></td>
</tr>
<tr>
<td>July 16, 2013, 10:26 a.m.</td>
<td>Weird Task</td>
<td>jgreen</td>
<td></td>
<td><input id="5Checkbox" type="checkbox" checked="yes" onclick="toggleCompletion(5);" /></td>
</tr>
<tr>
<td>July 16, 2013, 10:28 a.m.</td>
<td>Add another Task</td>
<td>jgreen</td>
<td>aaa</td>
<td><input id="2Checkbox" type="checkbox" value="False" onclick="toggleCompletion(2);" /></td>
</tr>
<tr>
<td>July 16, 2013, 10:29 a.m.</td>
<td>Add yet another Task</td>
<td>jgreen</td>
<td>asdfasdf</td>
<td><input id="3Checkbox" type="checkbox" value="False" onclick="toggleCompletion(3);" /></td>
</tr>
<tr>
<td>July 16, 2013, 12:21 p.m.</td>
<td>The Last Task</td>
<td>jgreen</td>
<td></td>
<td><input id="4Checkbox" type="checkbox" value="False" onclick="toggleCompletion(4);" /></td>
</tr>
</table>
<div class="indent" style="width:20%;overflow:hidden;margin-top:2px">
<form style="float:left" method="post" action="/maintenance/index/">
<input type='hidden' name='csrfmiddlewaretoken' value='3vJTLWV0aIqAYfPIX2toBSPPFTVnE7Ll' />
<input type="submit" value="Back" />
</form>
<form style="" method="post" action="/maintenance/tasks/2/addTask/">
<input type='hidden' name='csrfmiddlewaretoken' value='3vJTLWV0aIqAYfPIX2toBSPPFTVnE7Ll' />
<input type="submit" value="Add Task" />
</form>
</div>
</body>
后:
<head>
<title>Task List</title>
<style>
table {
margin-left: 100;
margin-top: 15;
}
h1 {
margin-left: 100;
margin-top: 75;
}
h3 {
margin-left: 100;
margin-top: 50;
}
.indent {
margin-left: 100;
}
.navigator {
position:absolute;
right:25px;
top:15px;
}
.navigator-panel {
position:absolute;
right:25px;
top:60px;
}
.user-info {
position:absolute;
left:25px;
top:15px;
}
.logout {
position:absolute;
left:25px;
top:60px;
}
</style>
<script>
//redirects to toggleCompletion view to toggle the value of one of the tasks isDone field
function toggleCompletion(task_id){
alert(task_id);
location.href = "/maintenance/tasks/2/toggleCompletion/" + str(task_id) + "/";
//window.location.replace("/maintenance/tasks/2/toggleCompletion/" + str(task_id) + "/");
}
</script>
<h1>Task List for July 2013 Maintenance</h1>
</head>
<!--Create a User info panel-->
<p class="user-info">You're logged in as jgreen</p>
<form class="logout" method="post" action="/maintenance/logoutResolve/">
<input type='hidden' name='csrfmiddlewaretoken' value='3vJTLWV0aIqAYfPIX2toBSPPFTVnE7Ll' />
<input type="submit" value="Logout" />
</form>
<!--Display table of the Maintenances-->
<body>
<table border="2">
<tr>
<th>Time</th>
<th>Task Description</th>
<th>Owner</th>
<th>Notes</th>
<th>Done?</th>
</tr>
<tr>
<td>July 16, 2013, 10:25 a.m.</td>
<td>Add a Task</td>
<td>jgreen</td>
<td>adlfjalkds</td>
<td><input id="1Checkbox" type="checkbox" value="False" onclick="toggleCompletion(1);" /></td>
</tr>
<tr>
<td>July 16, 2013, 10:26 a.m.</td>
<td>Weird Task</td>
<td>jgreen</td>
<td></td>
<td><input id="5Checkbox" type="checkbox" checked="yes" onclick="toggleCompletion(5);" /></td>
</tr>
<tr>
<td>July 16, 2013, 10:28 a.m.</td>
<td>Add another Task</td>
<td>jgreen</td>
<td>aaa</td>
<td><input id="2Checkbox" type="checkbox" value="False" onclick="toggleCompletion(2);" /></td>
</tr>
<tr>
<td>July 16, 2013, 10:29 a.m.</td>
<td>Add yet another Task</td>
<td>jgreen</td>
<td>asdfasdf</td>
<td><input id="3Checkbox" type="checkbox" value="False" onclick="toggleCompletion(3);" /></td>
</tr>
<tr>
<td>July 16, 2013, 12:21 p.m.</td>
<td>The Last Task</td>
<td>jgreen</td>
<td></td>
<td><input id="4Checkbox" type="checkbox" value="False" onclick="toggleCompletion(4);" /></td>
</tr>
</table>
<div class="indent" style="width:20%;overflow:hidden;margin-top:2px">
<form style="float:left" method="post" action="/maintenance/index/">
<input type='hidden' name='csrfmiddlewaretoken' value='3vJTLWV0aIqAYfPIX2toBSPPFTVnE7Ll' />
<input type="submit" value="Back" />
</form>
<form style="" method="post" action="/maintenance/tasks/2/addTask/">
<input type='hidden' name='csrfmiddlewaretoken' value='3vJTLWV0aIqAYfPIX2toBSPPFTVnE7Ll' />
<input type="submit" value="Add Task" />
</form>
</div>
</body>