CreateView + 重新定义 form_valid = 没有 return HttpResponse 对象
CreateView + redefinition of form_valid = didn't return an HttpResponse object
Django 1.10强文本
你能帮我理解为什么会导致:
视图 wiki.views.WikiCreate 没有 return HttpResponse 对象。它 return 改为 None。
post: 我在重新定义 post 方法时没有干扰继承链。我刚刚截取了url.
的参数
form_valid: 我 return 编辑了 HttpResponse。但似乎后来在某个地方消失了。
urlpatterns = [
url(r'^(?P<model>[-\w]+)/(?P<pk>\d+)/wiki/create/$', WikiCreate.as_view(), name='wiki_create'),
]
class WikiCreate(CreateView):
model = Wiki
fields = ['article']
def post(self, request, *args, **kwargs):
#For wiki history.
self.author = request.user
# Parameters from url reflect fields not shown to user.
self.related_model = kwargs.get('model')
self.related_object_id = kwargs.get('pk')
super(WikiCreate, self).post(request, *args, **kwargs)
def form_valid(self, form):
"""
Only "article" field was shown to tue user.
Now we save fields that were not explicitly shown (but were in the url).
"""
self.object = form.save(commit=False)
self.object.related_model = self.related_model
self.object.related_id = self.related_object_id
self.object.save()
return HttpResponseRedirect(self.get_success_url())
您没有 return 来自 post
中超级调用的响应。
请注意,覆盖该方法很少是个好主意,您当然不需要在这里。您可以直接在 form_valid
.
中访问这些 kwargs
Django 1.10强文本
你能帮我理解为什么会导致:
视图 wiki.views.WikiCreate 没有 return HttpResponse 对象。它 return 改为 None。
post: 我在重新定义 post 方法时没有干扰继承链。我刚刚截取了url.
的参数form_valid: 我 return 编辑了 HttpResponse。但似乎后来在某个地方消失了。
urlpatterns = [
url(r'^(?P<model>[-\w]+)/(?P<pk>\d+)/wiki/create/$', WikiCreate.as_view(), name='wiki_create'),
]
class WikiCreate(CreateView):
model = Wiki
fields = ['article']
def post(self, request, *args, **kwargs):
#For wiki history.
self.author = request.user
# Parameters from url reflect fields not shown to user.
self.related_model = kwargs.get('model')
self.related_object_id = kwargs.get('pk')
super(WikiCreate, self).post(request, *args, **kwargs)
def form_valid(self, form):
"""
Only "article" field was shown to tue user.
Now we save fields that were not explicitly shown (but were in the url).
"""
self.object = form.save(commit=False)
self.object.related_model = self.related_model
self.object.related_id = self.related_object_id
self.object.save()
return HttpResponseRedirect(self.get_success_url())
您没有 return 来自 post
中超级调用的响应。
请注意,覆盖该方法很少是个好主意,您当然不需要在这里。您可以直接在 form_valid
.