• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

BaseDeleteView抛出AttributeError(render_to_response missing)

django 来源:Christo Buschek 14次浏览

我尝试基于BaseDeleteView实现一个视图,该视图充当前端到REST后端的网站。双方通过HTTP请求进行通信。我想实现的是,我发送一个GET请求到激活URI(注册后通过每封电子邮件发送)。在这个视图中,我首先发送一个HTTP请求到后端,然后从前端的数据库中删除激活对象。我不想要确认页面,所以​​是不可能的。BaseDeleteView抛出AttributeError(render_to_response missing)

class ActivationView(BaseDeleteView): 
    success_url = "/activation/success/" 

    def get_object(self, queryset=None): 
     uuid = self.kwargs['uuid'] 
     try: 
      obj = AccountRegistration.objects.get(uuid=uuid) 
     except ObjectDoesNotExist: 
      raise Http404('Registration not found.') 

     return obj 

    def delete(self, request, *args, **kwargs): 
     obj = self.get_obj() 

     if obj.expire_date < datetime.now(): 
      obj.delete() 
      raise Http404('Registration expired.') 

     # send a http request to the backend 
     t = Transaction('/activate/%s/' % obj.account_name) 
     t.emit() 

     # delete the object 
     obj.delete() 

     # and redirect the request 
     return HttpResponseRedirect(self.get_success_url()) 

urls.py看起来像这样:

url(r'^activate/(?P<uuid>\w+)/$', 
    ActivationView.as_view(), name="account-activate"), 

,但我得到了以下错误:

Traceback (most recent call last): 
    File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 68, in __call__ 
    return self.application(environ, start_response) 
    File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 272, in __call__ 
response = self.get_response(request) 
    File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/core/handlers/base.py", line 169, in get_response 
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info()) 
    File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception 
    return debug.technical_500_response(request, *exc_info) 
    File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response 
    response = callback(request, *callback_args, **callback_kwargs) 
    File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/views/generic/base.py", line 47, in view 
    return self.dispatch(request, *args, **kwargs) 
    File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in dispatch 
    return handler(request, *args, **kwargs) 
    File "/home/crito/.pythonbrew/venvs/Python-2.7.2/thirty-web/lib/python2.7/site-packages/django/views/generic/detail.py", line 100, in get 
    return self.render_to_response(context) 
AttributeError: 'ActivationView' object has no attribute 'render_to_response' 

在我眼里它不应该叫连选择render_to_response。有任何想法吗?

===========解决方案如下:

您从BaseDeleteView继承而来,其中the documentation指出,不包括TemplateResponseMixin – 即所有与呈现响应有关的位。

改为从​​继承。


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)