我想在删除后删除数据库content.bt utl转到http://127.0.0.1:8004/login/delete_detail/6/ ..我可以重定向到success.html,即在同一页面
class DeleteView(generic.TemplateView):
template_name = 'success.html'
success_url='/login/success'
def get_context_data(self, *args, **kwargs):
context = super(DeleteView,self).get_context_data(**kwargs)
did = self.kwargs['did']
q_obj = Quest.objects.filter(id=did)
q_obj.delete()
答案 0 :(得分:0)
您可以使用get_success_url
方法:
from django.urls import reverse_lazy
class DeleteView(generic.TemplateView):
template_name = 'success.html'
success_url='/login/success'
def get_context_data(self, *args, **kwargs):
context = super(DeleteView,self).get_context_data(**kwargs)
did = self.kwargs['did']
q_obj = Quest.objects.filter(id=did)
q_obj.delete()
def get_success_url(self, **kwargs):
return reverse_lazy('delete_detail', kwargs = {'pk': self.kwargs['did']})
您也可以使用DeleteView
class:
TemplateView
class QuestDelete(DeleteView):
model = Quest
pk_url_kwarg = 'did'
def get_success_url(self, **kwargs):
return reverse_lazy('delete_detail', kwargs = {'pk': self.kwargs['did']})
要使用url的名称,您需要在urls.py
文件中的url模式中添加name参数,如下所示:
urlpatterns = [
path('delete_detail', views.delete_detail, name='delete_detail'),
]
答案 1 :(得分:0)
您应该覆盖get_success_url
方法。例如:
def get_success_url(self):
return reverse_lazy('delete-success')
另外,请尝试在success_url
success_url = reverse_lazy('delete-success')