如何在DeleteView中访问相关的对象ID

时间:2014-11-24 19:38:03

标签: django django-views

我有一个DeleteView。其中我想访问相关的对象ID。当用户发出POST请求时。

查看:

class DeletePacketView(LoginRequiredMixin, DeleteView):

    model = Packet
    template_name = DELETE_PACKET_TEMPLATE

    def get_success_url(self):

        # Return to customer home
        return reverse('myapp:view_customer',
                       kwargs={'pk': self.object.customer.id
                               }
                       )

    def post(self, request, *args, **kwargs):

        html = """
               <html>
                   <head>
                       <script>window.onunload = refreshParent; function refreshParent() {
                               window.opener.location.href=%s;
                               }
                       </script>
                   </head>
                   <body>
                   <h1>object deleted successfully</h1>
                   <button type="button" onclick="window.close()">OK</button>
                   </body>
               </html>
               """ % reverse('myapp:view_customer',
                              kwargs={'pk': self.object.customer.id
                                      }
                              )
        super(DeletePacketView, self).post(request, *args, **kwargs)
        return HttpResponse(html)

但是当我尝试提交表单时,我收到了错误。

回溯:

Environment:    

Request Method: POST
Request URL: http://localhost:8000/delete/packet/96/

Django Version: 1.7
Python Version: 2.7.8
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'myapp')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.contrib.admindocs.middleware.XViewMiddleware')


Traceback:
File "C:\Users\PrImE\envs\ge\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\PrImE\envs\ge\lib\site-packages\django\views\generic\base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "C:\Users\PrImE\envs\ge\lib\site-packages\django\utils\decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "C:\Users\PrImE\envs\ge\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "C:\Users\PrImE\envs\ge\lib\site-packages\django\utils\decorators.py" in bound_func

  23.         return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)
File "C:\Users\PrImE\envs\ge\lib\site-packages\django\views\generic\base.py" in dispatch
  87.         return handler(request, *args, **kwargs)
File "C:\Users\PrImE\workspace\myproject\repo\dev\myproject\myproject\myapp\views\delete_views\delete_packet_view.py" in post
  66.                               kwargs={'pk': self.object.customer.id

Exception Type: AttributeError at /delete/packet/96/
Exception Value: 'DeletePacketView' object has no attribute 'object'

我收到此错误,因为该对象已被删除。但是get_success_url方法可以正常工作,它将用户重定向到客户页面,为什么post方法不起作用?如何访问self.object.customer.id ???

1 个答案:

答案 0 :(得分:5)

def post(self, request, *args, **kwargs):
    self.object = self.get_object() #Add this to load the object
    #Your code
    return HttpResponse(html)

PS:请使用Django模板;)