我制作了我的第一个Django网站,其中有一个应用程序到目前为止命名为客户端,我将模板保存在project_name / clients / templates / clients中。
就像标题所说,我能够在我的视图文件中使用UpdateView更新我的模型客户端的实例,我想我可以使用DeleteView以相同的方式删除实例,但是我收到错误声明先前。任何帮助,将不胜感激。我看过其他类似的帖子,但没有任何帮助我解决这个问题
这是我的网址文件:
from django.conf.urls import url
from . import views
app_name = 'clients'
url(r'^$', views.IndexView.as_view(), name='index'),
# /clients/11/... could be any number
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
# /clients/viewed/
url(r'^viewed/', views.viewed, name='viewed'),
# /clients/add/
url(r'^add/$', views.ClientCreate.as_view(), name='client-add'),
# /clients/3/update/
url(r'^(?P<pk>[0-9]+)/update$', views.ClientUpdate.as_view(), name='client-update'),
# /clients/8/delete/
url(r'^(?P<pk>[0-9]+)/delete/$', views.ClientDelete.as_view(), name='client-delete'),
]
这是我的views.py中的相关课程:
from __future__ import unicode_literals
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import render
from .models import Client
class ClientUpdate(UpdateView):
model = Client
fields = ['name', 'age', 'height', 'weight',
'history_of_head_trauma', 'profession', 'is_athlete', 'email']
success_url = reverse_lazy('clients:index')
class ClientDelete(DeleteView):
model = Client
success_url = reverse_lazy('clients:index')
这是我的index.html中包含更新和删除按钮的div:
<div class="panel-footer">
<a type="button" class="btn btn-sm btn-success"><i class="glyphicon glyphicon-envelope"></i></a>
<span class="pull-right">
<a href="{% url 'clients:client-update' pk=client.id %}" type="button" class="btn btn-small btn-info"><i class="glyphicon glyphicon-edit"></i></a>
<a href="{% url 'clients:client-delete' pk=client.id %}" type="button" class="btn btn-small btn-danger"><i class="glyphicon glyphicon-remove"></i></a>
</span>
</div>
答案 0 :(得分:2)
出于某种原因,在使用UpdateView时,html中的按钮可以只是一个简单的链接,href指向正确的url,在我的情况下,我的视图文件中指向ClientUpdate ...
但对于DeleteView,html必须位于<form>
。这是我必须改变以使其工作的唯一代码。我基本上把表格放在了
<a href="{% url 'clients:client-delete' pk=client.id %}"....>
<div class="panel-footer">
<a type="button" class="btn btn-sm btn-success"><i class="glyphicon glyphicon-envelope"></i></a>
<span class="pull-right">
<a href="{% url 'clients:client-update' pk=client.id %}" type="button" class="btn btn-small btn-info"><i class="glyphicon glyphicon-edit"></i></a>
<form action="{% url 'clients:client-delete' pk=client.id %}" method="post" style="display: inline;">
{% csrf_token %}
<input type="hidden" name="client_id" value="{{ client.id }}"/>
<button type="submit" class="btn btn-danger btn-small">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
</span>
答案 1 :(得分:1)
默认情况下,DeleteView
会显示获取请求的确认页面,并删除发布请求的对象。您需要在视图上创建模板clients/client_confirm_delete.html
(或设置template_name
)以处理GET请求。有一个示例模板in the docs:
<form action="" method="post">{% csrf_token %}
<p>Are you sure you want to delete "{{ object }}"?</p>
<input type="submit" value="Confirm" />
</form>
您的另一个选择是在索引页面中添加表单,以便用户向删除页面提交发布请求。请注意,这意味着将立即删除该对象而不进行确认。即使你这样做,最好为GET请求添加一个模板,否则当用户/机器人直接导航到你的删除网址时你可能会收到错误。