Home > Blockchain >  Combine today date and input type=time in DateTimeField in Django
Combine today date and input type=time in DateTimeField in Django

Time:11-03

Here I am using Django 3.0 and Python 3.7

Here I am getting time from django template and i need to combine this time and today date as save it in database as DateTimeField

Here is my models.py

class WorkTime(models.Model):
    client = models.ForeignKey(Client,on_delete=models.CASCADE)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    total_time = models.TimeField(blank=True, null=True)

Here is my views.py

class AddWorkTimeView(View):
    def get(self, request):
        client = request.user.client
        return render(request,'core/work_time_form.django.html')


    def post(self, request):
        c = request.user.client
        start_time = request.POST.get('start_time')   # print start_time - 11:15     
        end_time = request.POST.get('end_time') # print end_time - 14:15

        WorkTime.objects.create(client=c,start_time=start_time,end_time=end_time)
        return redirect('work_times')

Here is my work_time_form.django.html

<form  method="post">
        {% csrf_token %}
        <div >
            <div >
                
                    <div >
                        <label >Start Time</label>
                        <input type="time" step="900"  name="start_time">
                    </div>
                   
                    <div >
                        <label >End Time</label>
                        <input type="time" step="900"  name="end_time">
                    </div>
                    <div id="form-buttons-container" >
                        <div >
                            <input type="submit"  value="Save">
                        </div>
                    </div>
                    
                
            </div>
        </div>
    </form> 
   

Here what format i want it to save to my datebase

Example:  2020-11-03 10:30:00 (here date is today date)

And also calculate the time difference between start_time and end_time in minutes and save it to total_time field

To achieve this what changes i need to do to my code

Please help me to solve this issue

CodePudding user response:

Use enter image description here

Output (admin panel)

enter image description here

  • Related