I have no idea and I need to ask your advice.
I have simple form:
class CommentForm(ModelForm): class Meta: model = Comment fields = ['text','author','news'] I want to add form in DetailView and hadle this form there:
class NewsDetailView(DetailView): model = News template_name = 'news/detail.html' def get_initial(self): return {'news': self.get_object(), 'author': self.request.user} def get_context_data(self, **kwargs): context = super(NewsDetailView, self).get_context_data(**kwargs) context['form'] = CommentForm(initial=self.get_initial()) return context def post(self, request, *args, **kwargs): ''' comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment_form.save() I don't want to show 'author' and news fieds. But if I hide them I can't to get initial values..
UPDATED:
After form validation I need return current form instance in the template through updating page. I attempted the next:
comment_form = CommentForm(request.POST, request=request) if comment_form.is_valid() and comment_form.validate_user(): comment_form.save() return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: context = super(NewsDetailView,self).get_context_data(**kwargs) context['form'] = comment_form return self.render_to_response(context) But did not work.
HiddenInputtextfield, and that will end as an invalid form since you are definingfields = ['text','author','news']in theMetaclass of your form.