1 回答

TA貢獻1853條經(jīng)驗 獲得超18個贊
改變你的form_valid(...)方法,
from django.http import HttpResponseRedirect
class CommentCreate(LoginRequiredMixin, CreateView):
model = Comment
form_class = forms.CommentForm
def form_valid(self, form):
comment = form.save(commit=False)
comment.author = self.request.user
comment.halp = Halp.objects.get(slug=self.kwargs['slug'])
comment.save()
self.object = comment
return HttpResponseRedirect(self.get_success_url())
def get_success_url(self, **kwargs):
return reverse_lazy('forum:halp-detail', kwargs={'slug': self.kwargs['slug']})
添加回答
舉報