我尝试在完成表单后将用户重定向到另一个页面而不保存。但是,当我使用反向功能时,我收到错误:'str' object has no attribute 'get'
在下面基于类的视图中,self.object.thing_id打印它应该的数据。
基于类的视图:
class ThingUpdateView(LoginRequiredMixin, UpdateView):
def form_valid(self, form):
if self.object.status != 'initiated':
print(self.object.thing_id)
return reverse('thing:detail', kwargs={'thing_id': str(self.object.thing_id) })
return super().form_valid(form)
即使反向(' thing:list')也会出现相同的错误
App Things' urls.py
url(r'^$', ThingListView.as_view(), name='list'),
url(r'^(?P<thing_id>[0-9A-Za-z]+)/$', ThingDetailView.as_view(), name='detail'),
url(r'^update/(?P<thing_id>[0-9A-Za-z]+)/$', ThingUpdateView.as_view(), name='update'),
当用户点击html页面上的提交按钮时,会发生此错误。它启动表单,然后路由到form_valid部分。 self.objectstatus!=&#39;发起了&#39;所以它试图反过来。
这是堆栈跟踪:
Traceback:
File "C:\Users\Starlord\Dev\ecommerce\lib\site-packages\django\core\handlers\exception.py" in inner
41. response = get_response(request)
File "C:\Users\Starlord\Dev\ecommerce\lib\site-packages\django\utils\deprecation.py" in __call__
142. response = self.process_response(request, response)
File "C:\Users\Starlord\Dev\ecommerce\lib\site-packages\django\middleware\clickjacking.py" in process_response
32. if response.get('X-Frame-Options') is not None:
Exception Type: AttributeError at /things/update/vd2dqvw3nw/
Exception Value: 'str' object has no attribute 'get'
答案 0 :(得分:1)
使用HttpResponseRedirect
代替
from django.http import HttpResponseRedirect
return HttpResponseRedirect(reverse('thing:detail', kwargs={'thing_id':self.object.thing_id)