1

Is any way to create hidden form field in CreateView form?

class CommentAdd(AjaxableResponseMixin, CreateView):
    model = Comment
    fields = ['author_name', 'text']
    success_url = '/thanks/'
    template_name = 'tree.html'

I have to pass some data (parent of comment) to the database. It works good with ModelForm, I pass parent with JS to the hidden field:

widgets = {'parent': widgets.HiddenInput}

How to do the same with CreateView form?

Eugene
  • 17
  • 5
  • possible duplicate of [Django: How to set a hidden field on a generic create view?](http://stackoverflow.com/questions/21652073/django-how-to-set-a-hidden-field-on-a-generic-create-view) – kylieCatt Jul 01 '15 at 12:24

1 Answers1

2

Define a model form class that includes the hidden input.

class CommentForm(ModelForm):
    class Meta:
        model = Comment
        fields = ('author_name', 'text', 'parent')
        widgets = {
            'parent': forms.HiddenInput,
        }

Then use that form in your view using the form_class attribute.

class CommentAdd(AjaxableResponseMixin, CreateView):
    form_class = CommentForm
    ...
zurfyx
  • 31,043
  • 20
  • 111
  • 145
Alasdair
  • 298,606
  • 55
  • 578
  • 516