我对Django比较新。我想从填充表单中保存数据,这是填充到timesheet.html中的数据到数据库然后检索数据并将其显示到list_timetable.html的表中。但是我被困住了,并且会在views.py" query_results = ltimesheet.objects.all()"上获得属性错误。
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 - 用于显示表格中的数据
{% 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
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
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)
答案 0 :(得分:0)
这是因为python是区分大小写的,你使用的是函数名(ltimesheet)而不是模型名(LTimesheet),还有渲染数据,你需要将它们传递给你的html文件
将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)