当数据库内容删除时,如何使用url参数传递方法重定向到同一页面?

时间:2018-05-07 06:51:37

标签: django

我想在删除后删除数据库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()

2 个答案:

答案 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')