Djangoは比較的新しいです。私は、fillheetからデータを保存したいと思います。これは、timesheet.htmlに入っているデータがデータベースに格納され、次にデータを取得し、それをlist_timetable.htmlという表に表示します。しかし、私は立ち往生し、views.py "query_results = ltimesheet.objects.all()"で属性エラーを取得します。塗りつぶしフォームからデータを取得してデータベースに保存し、表形式で表示するDJANGO
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)
にデータを表示するために使用
完全なエラーメッセージ –