0

I had following FormView which worked for what I wanted to do back then:

class LearningObjectiveView( LoginRequiredMixin, FormView ):
  form_class = LearningObjectiveForm
  template_name = 'learningobjective'
  success_url = reverse_lazy( 'learning_objective' )

  def get_context_data( self, **kwargs ):
    trainee = Trainee.objects.get( user = self.request.user )
    context = super( LearningObjectiveView, self ).get_context_data( **kwargs )
    context['learningobjective'] = LearningObjective.objects.filter( trainee = trainee.id )
    context['topic'] = Topic.objects.filter( trainee = trainee.id )
    return context

  def get_form_kwargs( self ):
    kwargs = super( LearningObjectiveView , self ).get_form_kwargs()
    kwargs['user'] = self.request.user
    return kwargs 

  def form_valid( self, form ):
    self.object = form.save( commit = False )
    if self.request.user.is_authenticated():
      self.object.trainee = Trainee.objects.get( user = self.request.user )
    self.object.save()
    form.save_m2m()
    return super( LearningObjectiveView, self ).form_valid( form )

Since I want to include pagination now and I just saw example on every CBV but the FormView, I wanted to use a ListView with a FormMixin.

ListView with FormMixin

class LearningObjectiveView( LoginRequiredMixin, FormMixin, ListView ):
  form_class = LearningObjectiveForm
  template_name = 'learningobjective'
  model = LearningObjective
  success_url = reverse_lazy( 'learning_objective' )

  def get_context_data( self, **kwargs ):
    trainee = Trainee.objects.get( user = self.request.user )
    context = super( LearningObjectiveView, self ).get_context_data( **kwargs )
    context['learningobjective'] = LearningObjective.objects.filter( trainee = trainee.id )
    context['topic'] = Topic.objects.filter( trainee = trainee.id )
    return context

  def get_form_kwargs( self ):
    kwargs = super( LearningObjectiveView , self ).get_form_kwargs()
    kwargs['user'] = self.request.user
    return kwargs 

  def form_valid( self, form ):
    self.object = form.save( commit = False )
    if self.request.user.is_authenticated():
      self.object.trainee = Trainee.objects.get( user = self.request.user )
    self.object.save()
    form.save_m2m()
    return super( LearningObjectiveView, self ).form_valid( form )

Apparently this only renders the context data but not the form I want to use. Do you know how to solve this problem.

ZedsWhatSheSaid
  • 467
  • 2
  • 9
  • 23

1 Answers1

2

FormMixin doesn't do what you think it does. All it does is provide methods for getting and instantiating the form; it doesn't add it to the context, nor does it deal with validating and saving it on submission.

You might want to use ProcessFormView instead - as the docs state, despite the name this is really a mixin. However you should also bear in mind the warning on the mixins documentation against trying to mix together mixins or views from different generic groups.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thank you very much for your answer. Despite of that, can you please tell me how you would program a solution for this approach. ( Paginated list and form on the same site ). Better practices and solutions are welcomed :) – ZedsWhatSheSaid Mar 24 '15 at 15:56