0
Django的1.10 強大的文本CreateView的+ form_valid =的重新定義沒有返回HttpResponse對象
你能幫我明白爲什麼這會導致:
視圖wiki.views.WikiCreate沒」返回一個HttpResponse對象。它返回None而不是。
post:重新定義post方法時,我並沒有干涉繼承鏈。我剛剛截獲了來自url的參數。
form_valid:我返回了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())
非常感謝。 – Michael