BaseDeleteView抛出AttributeError(render_to_response missing)
我尝试基于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
- 即所有与呈现响应有关的位。
改为从继承。
回答:
如果你想省略确认页面,只需用POST直接调用你的DeleteView即可。这是最理想的,因为删除对象应该由csrf保护。
以上是 BaseDeleteView抛出AttributeError(render_to_response missing) 的全部内容, 来源链接: utcz.com/qa/260927.html