将值保存到数据库Django中

时间:2017-06-28 11:20:04

标签: python django database forms

嗨,我对Django比较新。我有一个时间表供用户输入详细信息,当选择提交按钮时,这些填充字段中的数据将保存到数据库中。但我不知道为什么我的工作不起作用。随着值的保存,它将被检索并显示在另一个html文件的表格中。

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

1 个答案:

答案 0 :(得分:1)

正如Fazil已经说过的那样,请查看文档。但是为了开始给你一点帮助: 您缺少的是views.py中的表单验证,如:

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) 

form.save命令将表单保存到数据库中。

您的<form>标记缺少该方法! (它应该是POST)。

我建议遵循一个教程(来自django docs或任何其他教程的教程),这样你就可以了解事情是如何运作的。

在一般情况下: 您的问题(特别是代码)越短,有人愿意阅读它的可能性就越大。应该删除"<br>"之类的内容。没有人关心你的形式。你的JS部分也应该被删除,因为它与问题无关。