我有以下表格显示用户设置的模型条目。选中后,我希望按钮抓住它的pk并将其发送到Delete视图。这是当前的代码,但我遗漏了这一部分。
user_detail模板html
<form id="SettingsListForm"><label>  Settings List :      </label>
{% if user.usersetting.first %}
<select class="form-control" name="settingslist" id = "settingslist" form="SettingsListForm" >
{% for settings in user.usersetting.all %}
<option value="{{ settings.file.url }}">{{ settings }}
</option>
{% endfor %}
</select>
{% else %}
<li class="list-group-item">NO SETTINGS YET</li>
{% endif %}
<button class="btn btn-outline-light btn-circle"><i class="glyphicon glyphicon-minus" href="{% url 'my_app:setting_delete' pk=user.usersetting.last.id %}"></i></button>
{% block delete_setting_confirm_block %}
{% endblock %}
</form>
setting_confirm_delete html模板,包含delete_setting_confirm_block
{% extends 'login_app/user_detail.html' %}
{% block delete_setting_confirm_block %}
<h4>
ARE YOU <b>REALLY</b> SURE YOU WANT TO <b>DELETE</b> THIS SETTING ?
<form method="POST">
{% csrf_token %}
<button type="submit" class="btn btn-outline-light btn-danger" value="Delete">YES</button>
<a class="btn btn-outline-light btn-default" href="{% url 'login_app:user_detail' pk=user.id %}"><b>NO</b></a>
</form>
</h4>
{% endblock %}
my_app网址
url(r'^setting/(?P<pk>\d+)/$',views.UserSettingDeleteView.as_view(),name='setting_delete'),
my_app视图中的UserSettingDeleteView
class UserSettingDeleteView(DeleteView):
model = models.UserSetting
template_name = 'my_app/setting_confirm_delete.html'
def get_success_url(self):
return reverse('my_app:user_detail', kwargs={'pk': self.object.user.pk})
不知何故,类似的技术在使用listgroups时工作正常:
user_detail html中的工作示例
<ul class="list-group">
{% if user.userdata.first %}
{% for data in user.userdata.all %}
<li class="list-group-item">{{ data }}<a class="btn btn-outline-light btn-circle" href="{% url 'my_app:data_delete' pk=data.pk %}"><i class="glyphicon glyphicon-remove"></i></a><a href="{{ data.file.url }}" class="btn btn-outline-light btn-circle" download><i class="glyphicon glyphicon-save"></i></a></li>
{% endfor %}
{% block delete_data_confirm_block %}
{% endblock %}
{% else %}
<li class="list-group-item">NOTHING RECORDED YET</li>
{% endif %}
</ul>
答案 0 :(得分:0)
在template.html
中,您应该创建一个删除表单,如下所示:
<form action="{% url 'my_app:setting_delete' pk=user.usersetting.last.id %}" method="post">
{% csrf_token %}
<input type="submit" value="Delete" class="btn btn-outline-light btn-circle">
</form>
因为在HTML格式中您无法直接发送PUT
或DELETE
,所以您应该通过POST
请求伪造它。阅读this会很有用,可以很好地解释。
您的UserSettingDeleteView
可以这么简单:
from django.views.generic.edit import DeleteView
from django.urls import reverse_lazy
class UserSettingDeleteView(DeleteView):
model = MyModel
# Replace with the model you want to delete (User)
success_url = reverse_lazy('my_app:setting_list')
# After deletion, possibly you will want to redirect the user to the list view
内置DeleteView
关注您通过您的网址传递的pk
参数找到您的模型并将其删除,您只需配置model
和{{1 }}}。
现在,如果您点击删除按钮,您应该会删除所需的条目,并将用户重定向到列表视图。
修改强>
我忘了你想通过选项获得这个条目。在这里,您必须使用一些JavaScript才能找到当前选定的元素,并将其success_url
发送到您的pk
。
首先,将DeleteView
添加到您的id
代码中,如下所示:
option
然后添加一些 jQuery :
<select class="form-control" name="settingslist" id="settingslist" form="SettingsListForm">
{% for settings in user.usersetting.all %}
<option value="{{ settings.file.url }}" id="{{ settings.id }}">{{ settings }}</option>
{% endfor %}
</select>
最后,您需要将var settingId = $('#SettingsListName').find(":selected").attr('id');
发送到相应的网址,但我对JavaScript不是很熟悉。
答案 1 :(得分:0)
尝试在表单中添加隐藏的输入。现在,没有任何东西通过表格。
<input type="hidden" name="del_setting" value="{{user.usersetting.last.id}}">
所以你的表格看起来像这样
<form method="POST">
{% csrf_token %}
<input type="hidden" name="del_setting" value="{{user.usersetting.last.id}}">
<button type="submit" class="btn btn-outline-light btn-danger" value="Delete">YES</button>
<a class="btn btn-outline-light btn-default" href="{% url 'login_app:user_detail' pk=user.id %}"><b>NO</b></a>
</form>
您也可能应该将其移出用户详细信息模板中的当前表单,这样您就不会在表单中放置表单:
{% block delete_setting_confirm_block %}
{% endblock %}
答案 2 :(得分:0)
前面提到的解决方案都没有用,所以我猜问题是嵌套的bootstrap选择项,它应该显示设置的pk
。
为简单起见,我使用list-group
代替
<ul class="list-group">
{% if user.usersetting.first %}
{% for settings in user.usersetting.all %}
<li class="list-group-item">{{ settings }}<a class="btn btn-outline-light btn-circle" href="{% url 'my_app:setting_delete' pk=settings.pk %}"><i class="glyphicon glyphicon-remove"></i></a></li>
{% endfor %}
<label>Create a new setting...</label>
<a class="btn btn-outline-light btn-circle"><i class="glyphicon glyphicon-plus"></i></a>
{% else %}
<li class="list-group-item">Create a new setting...<a class="btn btn-outline-light btn-circle"><i class="glyphicon glyphicon-plus"></i></a></li>
{% endif %}
{% block delete_setting_confirm_block %}
{% endblock %}
</ul>
作为一种解决方法,这很有效。