python - Django form_valid is not working -


i new django . can me code. trying calculate duration between 2 datefield save.

class employeecreate(createview):     model = employee     form_class = employeecreateform     success_url = "/employee-list/"      def form_valid(self, form):         self.object.total_leave = (self.object.to_date - self.object.from_date).days +1         self.object.save()         return httpresponseredirect(self.get_success_url()) 

there couple of issues

  1. you should use form.instance instead of self.object
  2. call super method

so update code as:

def form_valid(self, form):     self.instance.total_leave = (self.instance.to_date - self.instance.from_date).days +1     self.instance.save()     return super(employeecreate, self).form_valid(form) 

refer docs form handling class-based views


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -