在Django中以数据库形式保存ModelForm DateField

时间:2019-03-08 11:41:22

标签: python django django-models django-forms

我是Django的新手,我正在自学如何使用SQLite数据库后端开发完整的前端。 我正在尝试创建一个请假申请表,让用户请求请假,将请求保存在数据库中,让经理签名,一旦被接受,将请求保存为准予的请假请求。

我的 model.py 是:

from django.db import models


class StaffDetail(models.Model):

staff_member_name       = models.CharField(max_length=100)
deptartment             = models.CharField(max_length=50)
manager                 = models.CharField(max_length=100)
email_address           = models.CharField(max_length=100)
password                = models.CharField(max_length=100)
date_joined             = models.DateField(auto_now_add=True)
holidays_allowed        = models.DecimalField(default=25, max_digits=4, decimal_places=2)
holiday_taken           = models.DecimalField(default=0, max_digits=4, decimal_places=2)
# last_holiday          = models.DateField(null=True, blank=True)


def __str__(self):
    return self.staff_member_name



class HolidayRequest(models.Model):

leave_requested_by      = models.CharField(max_length=100)
leave_reason            = models.CharField(max_length=50)
other_reason            = models.CharField(max_length=100)
holiday_start           = models.DateField(null=True, blank=True)
holiday_end             = models.DateField(null=True, blank=True)
total_days              = models.DecimalField(max_digits=4, decimal_places=2)
# accept_or_rejected        = models.CharField(max_length=100)

def __str__(self):
    return self.leave_requested_by



class HolidayRecord(models.Model):

leave_requested_by      = models.ForeignKey(HolidayRequest,  on_delete=models.CASCADE)
# leave_reason          = models.CharField(max_length=50)
# other_reason          = models.CharField(max_length=100)
# holiday_start         = models.DateField()
# holiday_end               = models.DateField()
# total_days                = models.DecimalField(max_digits=4, decimal_places=2)
# leave_accept_by           = models.BooleanField(default=False)

def __str__(self):
    return self.leave_requested_by

forms.py

from django import forms
from .models import StaffDetail, HolidayRequest


class LRForm(forms.ModelForm):

# ACCEPT_REJECT_CHOICES =[
#                   'accepted', 'Accepted',
#                   'rejected', 'Rejected',
#                   ]
# LEAVE_REASON_CHOICES  =[
#                   'annual_leave', 'Annual Leave',
#                   'sick_leave', 'Sick Leave',
#                   'unpaid_leave', 'Unpaid_Leave',
#                   'other', 'Other',
                    # ]
leave_requested_by      = forms.CharField(max_length=100, label='Enter your full name:')
# email                 = forms.EmailField(label='Enter your email address:')
leave_reason            = forms.CharField(max_length=150, label='Reason for Leave(Annual Leave, Sick Leave, Unapid Leave, Other):')
other_reason            = forms.CharField(max_length=100, label= 'If you have selected the reason "Other", Please specify:')
leave_start             = forms.DateField(widget = forms.SelectDateWidget, label='Leave Start Date:')
leave_end               = forms.DateField(widget = forms.SelectDateWidget, label='Leave End Date:')
total_days              = forms.DecimalField(label='No. of total working days requesting:', max_digits=4, decimal_places=2)
# accept_or_rejected        = forms.ChoiceField(label='Reason for Leave:', widget=RadioSelect(choices=ACCEPT_REJECT_CHOICES))

class Meta:

    model               = HolidayRequest
    fields              = ('leave_requested_by', 'leave_reason', 'other_reason', 'leave_start', 'leave_end', 'total_days')

views.py

from django.shortcuts import render, redirect
from .forms import LRForm

def home_view(request):

context         = {}

return render(request, 'Lrf_App/home.html', context)


def form_view(request):

forms           = LRForm()
context         = {'form': forms}

if request.method =='POST':

    form        = LRForm(request.POST)

    if form.is_valid():

        print('In If loop')

        name                = form.cleaned_data['leave_requested_by']
        print(name)
        reason              = form.cleaned_data['leave_reason']
        print(reason)
        other_reason        = form.cleaned_data['other_reason']
        print(other_reason)
        start_date          = form.cleaned_data['leave_start']
        print(start_date)
        end_date            = form.cleaned_data['leave_end']
        print(end_date)
        days                = form.cleaned_data['total_days']
        print(days)

        form.save()

        return redirect('thanks')
    else:
        pass

return render(request, 'Lrf_App/form.html', context)

def thanks_view(request):

context         = {}

return render(request, 'Lrf_App/thanks.html', context)

forms.html

{% extends 'base.html' %}

{% block title %}Innvotek - Request Form{% endblock title%}

{% block content %}

    <form action="" method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" name="submit" value="Submit">
    </form>
{% endblock content %}

我正在尝试保存从用户输入的日期文件,但它根本不保存日期。

请有人帮忙!

0 个答案:

没有答案