简体   繁体   English

从填充表单中检索数据,然后保存到数据库中,然后以表格形式显示DJANGO

[英]Retrieve data from filled-form then save into database and then display in table form DJANGO

I am relatively new to Django. 我对Django比较陌生。 I would like to save data from a filled-form, which is the data being filled into timesheet.html into Database then retrieve data and display it into a table which is list_timetable.html. 我想从填充表格中保存数据,即将数据填充到timesheet.html中并存入数据库,然后检索数据并将其显示在list_timetable.html表中。 But I am stuck and will get attribute error on views.py "query_results = ltimesheet.objects.all()". 但是我被困住了,并且在views.py“ query_results = ltimesheet.objects.all()”上会出现属性错误。

timesheet.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 (!$('#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>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 %}

list_timesheet.html - used to display data in table list_timesheet.html-用于在表中显示数据

{% block body %}
<table>
    <tr>
        <th>Student ID</th>
        <th>Student Name</th>
        <th>Start Date</th>
        <th>End Date</th>
        <th>Status</th>
    </tr>
    { % for item in query_results % }
    <tr>
        <td>{{ltimesheet.timesheet}}</td>
        <td>{{ltimesheet.status}}</td>
    </tr>
    { % end for % }
</table>
<table id="example" class="display" cellspacing="0" width="100%">
{% endblock %}

views.py views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')

models.py models.py

from django.db import models

from django.http import HttpResponseRedirect
from django.shortcuts import render

#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

#consists of all the details of the timesheet under 'View Timesheets'        

class LTimesheet(models.Model):
timesheet = models.ForeignKey(Timesheet, on_delete=models.CASCADE)
status = models.CharField("Status", max_length=100)

This is because python is case sensitive and you are using function name (ltimesheet) instead of model name(LTimesheet), and also to render data you need to pass them to your html file 这是因为python区分大小写,并且您使用的是函数名(ltimesheet)而不是模型名(LTimesheet),并且要呈现数据,您需要将它们传递给html文件

Change your views.py file to: 将您的views.py文件更改为:

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
    query_results = LTimesheet.objects.all()
    data={query_results:query_results}
    return render(request, 'hrfinance/list_timesheet.html',data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM