Ghulam Haider
Ghulam Haider

Reputation: 35

Django form data not saving into databsae

I have a form on web app, using to get the data and store into database. The problem is: I am using different fields along with 2 Date Fields. When I removed the 2 date fields from the Model and Form, the form data stores easily into database but when added again into the Model and Form, it dose not save anything into database.

Model:

name = models.CharField('Name', max_length=50, help_text='Co-worker name.')
    email = models.EmailField('Email', help_text='Co-worker email.')
    address = models.TextField('Address', help_text='Co-worker address.')
    phone = models.CharField('Phone Number', max_length=20, help_text='Co-worker phone number.')
    companyName = models.CharField('Company Name', max_length=80, help_text='Co-worker company name.', null=True,
                                   blank=True)
    workingLocation = models.CharField('Working Location', max_length=50,
                                       help_text='Co-worker working '
                                                 'location.')
    workingShift = models.CharField('Working Shift', max_length=50, help_text='Co-worker working shift.', default='')
    workingSpace = models.CharField('Working Space', max_length=50, help_text='Co-worker working space.', default='')
    teamMembers = models.CharField('Team Members', max_length=15, help_text="Co-Worker's Team Size.", default='')
    coworkerPicture = models.ImageField('Co-Worker Picture', upload_to='../media/images/co-woker-pictures'
                                        , help_text='Co-worker Picture.', default='profile_pics/default.jpg', )
    joiningDate = models.DateField('Joining Date', help_text='Joining Date of Co-worker',default=date.today,
                                    )
    dob = models.DateField('Date of Birth', help_text='Date of Birth of Co-worker',  default=date.today,

View:

def Coworkers(request):
    # If user is not logged In
    if not request.user.is_authenticated:
        return redirect('login')

    if request.method == 'POST':
        form = addCoWorkerForm(request.POST, request.FILES)
        print(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, 'Co-Worker added successfully.')
            return redirect('admin/co-workers')

   c = context = ({
    'form': form,
    })

   # Render the HTML template co_workers.html
    return render(request, 'dashboard/co_workers.html', c)  

Form:

class addCoWorkerForm(forms.ModelForm):
    name = forms.CharField(widget=forms.TextInput(attrs={
        'class': 'form-control formInput',
        'placeholder': 'Name',
        'required': 'True'
    }))
    email = forms.EmailField(widget=forms.EmailInput(attrs={
        'class': 'form-control formInput',
        'placeholder': 'Email',
        'required': 'True'

    }))
    address = forms.CharField(widget=forms.Textarea(attrs={
        'class': 'form-control formInput',
        'placeholder': 'Address',
        'rows': '4'

    }))
    companyName = forms.CharField(widget=forms.TextInput(attrs={
        'class': 'form-control formInput',
        'placeholder': 'Company Name',

    }))
    phone = forms.CharField(widget=forms.TextInput(attrs={
        'class': 'form-control formInput',
        'placeholder': 'Phone Number',
        'required': 'True'
    }))
    CHOICES = [('Choose working location..', 'Choose working location..'),
               ('Islamabad', 'Islamabad'),
               ('Rawalpindi', 'Rawalpindi')]
    workingLocation = forms.ChoiceField(choices=CHOICES, widget=forms.Select(attrs={
        'class': 'form-control formInput',
        'required': 'True',
    }))
    CHOICES = [('Choose working shift..', 'Choose working shift..'),
               ('Day Time', 'Day Time'),
               ('Night', 'Night')]
    workingShift = forms.ChoiceField(choices=CHOICES, widget=forms.Select(attrs={
        'class': 'form-control formInput',
        'required': 'True',
    }))
    CHOICES = [('Choose working space..', 'Choose working space..'),
               ('Shared Space', 'Shared Space'),
               ('Private Room', 'Private Room')]
    workingSpace = forms.ChoiceField(choices=CHOICES, widget=forms.Select(attrs={
        'class': 'form-control formInput',
        'required': 'True',
    }))
    teamMembers = forms.CharField(widget=forms.TextInput(attrs={
        'class': 'form-control formInput',
        'placeholder': 'Team Members',
        'required': 'True'
    }))
    coworkerPicture = forms.ImageField(widget=forms.FileInput(attrs={
        'class': 'form-control formInput',
    }))
    joiningDate = forms.DateField(widget=forms.DateInput(attrs={
        'class': 'form-control formInput',
        'id': 'datePicker',
        'input_formats': ['%d/%m/%Y %H:%M']
    }))
    dob = forms.DateField(widget=forms.DateInput(attrs={
        'class': 'form-control formInput',
        'id': 'datePicker',
        'input_formats': ['%d/%m/%Y %H:%M']
    }))

       class Meta:
        model = CoWorker_Data
        fields = '__all__'

Please suggest me solution.

Upvotes: 1

Views: 54

Answers (1)

Arjun Shahi
Arjun Shahi

Reputation: 7330

Your input_formats is for DateTimeField but you have defined the field as only DateField so change here like this:

  joiningDate = forms.DateField(widget=forms.DateInput(attrs={
        'class': 'form-control formInput',
        'id': 'datePicker',
        input_formats= settings.DATE_INPUT_FORMATS
    }))

EDIT: If you are defining the date_input_format in your settings.py then define it like this

DATE_INPUT_FORMATS = ['%d/%m/%Y']

Note:DATE_INPUT_FORMATS in settings.py has no effect with USE_I18N = True.

Upvotes: 1

Related Questions