1

Hi i am relatively new to Django. I have a timesheet for user to enters the details, when the submit button is chosen, the data in those filled-up fields will be saved into the Database. But i have no idea why mine doesnt work. As the values will be saved then it will be retrieved and be displayed in a table from in another html file.

timesheet.html

{% extends 'hrfinance/base.html' %}
{% block title %} Timesheet {% endblock %}
{% block link %}
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'hrfinance/css/timesheet.css' %}"/>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
    /*to ensure that all the textbox and checkbox have been filled before users can submit*/
    function validation() {
        /*ensure start date has been entered*/
        if (!$('#studentid').val().trim()) {
            alert('Please fill in student ID field');
        }
        /*ensure start date has been entered*/
        if (!$('#studentname').val().trim()) {
            alert('Please fill in student name field');
        }
        /*ensure start date has been entered*/
        if (!$('#sdate').val().trim()) {
            alert('Please fill in start date field');
        }
        /*ensure end date has been entered*/
        if (!$('#edate').val().trim()) {
            alert('Please fill in end date field');
        }
        /*ensure checkbox has been ticked*/
        if (!$('#agree').is(':checked')) {
            alert('Please indicate that you have satisfied all the requirements');
        }
        else{
            console.log('ok')
      }
    }
</script>
{% endblock %}

{% block body %}
<div class="sub-bar">
    <p>Submit Timesheet</p>
</div>
<br>
<br>
<br>
<br>
<br>
<br>
<form onsubmit="return validation()">
    <div class="content-wrapper">
        <div class="sub-content">
            <div>
                <p>Student ID: {{timesheet.studentID}}</p>
                <input id="sid" type="field" name="studentid">
            </div>
        </div>

        <div class="sub-content">
            <div>
                <p>Student Name: {{timesheet.studentName}}</p>
                <input id="sname" type="field" name="studentname">
            </div>
        </div>

        <div class="sub-content">
            <div>
                <p>Start Date: {{timesheet.startDate}}</p>
                <input id="sdate" type="date" name="startdate">
            </div>
        </div>

        <div class="sub-content">
            <div>
                <p>End Date: {{timesheet.endDate}}</p>
                <input id="edate" type="date" name="enddate">
            </div>
        </div>
    </div>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <div class="end-content">
        <div class="center-align">
            <div class="checklist">
                <p>By checking this box I agree that I have satisfied all requirements to continue receiving my scholarship
            allowance.</p>
                <input id="agree" type="checkbox" name="checkbox" class="tick-att">
            </div>
            <br>
            <div class="align-right">
                <input type="submit" class="button" name="submit" value="submit" >
            </div>
      </div>
    </div>
</from>
{% endblock %}

views.py

def timesheet(request):
return render(request, 'hrfinance/timesheet.html')

models.py

#consists of all the details in the timesheet
class Timesheet(models.Model):
studentID = models.CharField("Student ID", max_length=8, primary_key=True, default="")
studentName = models.CharField("Student Name", max_length=500, default="")
startDate = models.DateField("Start Date", max_length=8)
endDate = models.DateField("End Date", max_length=8)

def __str__(self):
    return self.studentID
6
  • Is that all in your views.py?? Commented Jun 28, 2017 at 11:21
  • @FazilZaid yes... Commented Jun 28, 2017 at 11:23
  • Please have a look in this link ...docs.djangoproject.com/en/1.11/topics/forms Commented Jun 28, 2017 at 11:25
  • First thing first, get to know how to get form input data in django views. As of now, leave about javascript validation Commented Jun 28, 2017 at 11:34
  • I don't think you even need JS validation, if you set field in the model to be required. Commented Jun 28, 2017 at 11:38

1 Answer 1

1

As Fazil already said please take a look into the documentation. But to give you a little help for the start: What you are missing is the form validation in your views.py like:

def timesheet(request):
    user = request.user 
    form = yourform(request.POST or None)
    if form.is_valid():
        ....
        form.save()
    context = {
     'form':form,
     }
  return render(request, 'hrfinance/timesheet.html',context) 

the form.save command saves your form into the db.

Your <form> tag is missing the method! (it should be POST).

I would suggest to follow a tutorial (the one from the django docs or any other) so you have an Idea how things work.

In generell: The shorter your question (especially the Code) the more likely someone is willing to read it. Things like "<br>" should be cut out. Nobody cares for the style of your form. Your JS Part should be cut out as well since it is irrelevant for the question.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.