Django:从单选创建通用链接
我有一个index.html,它显示我拥有的所有数据库。现在我想给出一些处理数据库的选项(例如编辑、删除等)。所以我想用数据库ID和一些链接创建一个无线电选择,用于重定向正确的url 我的目标:点击请求的操作,将用户带到正确的url 我希望,这是可以理解的:) 谁能帮帮我吗 //编辑Django:从单选创建通用链接,django,django-templates,Django,Django Templates,我有一个index.html,它显示我拥有的所有数据库。现在我想给出一些处理数据库的选项(例如编辑、删除等)。所以我想用数据库ID和一些链接创建一个无线电选择,用于重定向正确的url 我的目标:点击请求的操作,将用户带到正确的url 我希望,这是可以理解的:) 谁能帮帮我吗 //编辑 <script> function edit_db(id){ window.location.href="/edit/"+id } function delete_db(id){ wi
<script>
function edit_db(id){
window.location.href="/edit/"+id
}
function delete_db(id){
window.location.href="/"+id+"/delete/"
}
</script>
<form method="post" action="">
<ul>
<table>
<tr>
<th> </th>
<th>Database</th>
<th>expireDate</th>
</tr>
{% for db in resources %}
<tr>
<td>
<input type="radio" name="id" id="{{db.id}}" value="{{db.id}}">
</td>
<td>{{db.name}}</td>
<td>{{db.expireDate}}</td>
</tr>
{% endfor %}
</table>
<input type="submit" class="submitButton" onclick="edit_db({{ db.id }})" value="{% trans "Edit database"%}" />
<input type="submit" class="submitButton" onclick="delete_db({{ db.id }})" value="{% trans "Delete database"%}" />
</form>
功能编辑数据库(id){
window.location.href=“/edit/”+id
}
函数delete_db(id){
window.location.href=“/”+id+“/delete/”
}
数据库
过期的
{资源%%中的数据库为%0}
{{db.name}
{{db.expireDate}
{%endfor%}
我建议您使用jQuery或其他工具来实现这一点
但是,您需要首先调整id,因为id/类不能以html中的数字开头,所以请使用id=“database”{{{db.id}}
我不知道{%show\u task\u link./{{db.id}/edit“{(“edit database”)%}
有什么功能,但请确保它有一个id,这样您就可以通过jQuery访问它,这类似于:
<script type="text/javascript>
$(function(){
$('.db').click(function(){
if($(this).is(':checked')){
$('#id_for_your_link').html() // put your link in html, add the id through $(this).val()
}
}
})
</script>
功能编辑数据库(id){
window.location.href=“/edit/”+id
}
函数delete_db(id){
window.location.href=“/”+id+“/delete/”
}
数据库
过期的
{资源%%中的数据库为%0}
{{db.name}
{{db.expireDate}
{%endfor%}
我使用了“$('database_{{db.id}}”).html(“/edit/”),但我仍然得到一个404错误,因为id仍然不在URL中。最好不要为所有单选按钮编写jQuery函数,最好编写一个为所有单选按钮提供服务的函数,就像我的一样。查看jquery文档了解更多信息。然后在.html()中,您的链接应该是:.html('./'+$(this.val()+'/edit');
'$('database{{{{db.id}}')).html('./'+$(this.val()+'/edit');)“=>调试:无效变量!同样的问题:url中缺少id,我得到一个404错误。它不起作用,因为你把它放在表单外部,结果没有id脚本必须在表单内部,或者你的意思是什么?我有点困惑,你说你想要单选,但在代码中你使用按钮。你真的想要什么?我使用单选按钮: ''
<script>
function edit_db(id){
window.location.href="/edit/"+id
}
function delete_db(id){
window.location.href="/"+id+"/delete/"
}
</script>
<form method="post" action="">
<ul>
<table>
<tr>
<th> </th>
<th>Database</th>
<th>expireDate</th>
</tr>
{% for db in resources %}
<tr>
<td>
<input type="radio" name="id" id="{{db.id}}" value="{{db.id}}">
</td>
<td>{{db.name}}</td>
<td>{{db.expireDate}}</td>
<td>
<input type="submit" class="submitButton" onclick="edit_db({{ db.id }})" value="{% trans "Edit database"%}" />
<input type="submit" class="submitButton" onclick="delete_db({{ db.id }})" value="{% trans "Delete database"%}" />
</td>
</tr>
{% endfor %}
</table>
</form>