如何在表格中显示值,因为现在只显示选项卡的标题

2024-04-24 17:25:51 发布

您现在位置:Python中文网/ 问答频道 /正文

你知道吗时间表.html基本上是一张表格来填写细节。我已经创建了一个列表_时间表.html列出所有正在填写的时间表。但我不知道我什么时候运行它,只显示这样的标题: {%表示查询中的项{u results%}{%表示%} 学号学名开始日期结束日期

列表_时间表.html

<form method="POST" class="display"  cellspacing="0" width="100%">
    {% csrf_token %}
    <table>
        <tr>
            <th> Student ID </th>
            <th> Student Name </th>
            <th> Start Date </th>
            <th> End Date </th>
        </tr>
        { % for item in query_results % }
        <tr>
            <td>{{item.studentID}}</td>
            <td>{{item.studentName}}</td>
            <td>{{item.startDate}}</td>
            <td>{{item.endDate}}</td>
        </tr>
        { % end for % }
    </table>
</form> 

时间表.html

<form method="POST" onsubmit="return validation()">
    {% csrf_token %}
    <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>
    <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>
</form>

型号.py

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)

视图.py

def timesheet(request):
if request.method == "POST":
    if 'submit' in request.POST:
        form = TimesheetForm(request.POST)
        if form.is_valid():
            timesheet = form.save(commit=False)
            timesheet.save()
            return HttpResponseRedirect(reverse('hrfinance/timesheet.html'))
    #if the form is not valid, redirect the student to the same page
        else:
            form = TimesheetForm()
            return render(request, 'hrfinance/timesheet.html', {'form': form})
else:
    form = TimesheetForm()
    return render(request, 'hrfinance/timesheet.html', {'form': form})

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

class TimesheetForm(forms.ModelForm):
class Meta:
    model = Timesheet
    fields = '__all__'

Tags: divforminputdatereturnmodelsrequesthtml
1条回答
网友
1楼 · 发布于 2024-04-24 17:25:51
def timesheet(request):
    if request.method == "POST":
         if 'submit' in request.POST:
             form = TimesheetForm(request.POST)
             if form.is_valid():
                 timesheet = form.save(commit=False)
                 timesheet.save()
                 return HttpResponseRedirect(reverse('hrfinance/timesheet.html'))
        #if the form is not valid, redirect the student to the same page
            else:
                form = TimesheetForm()
                return render(request, 'hrfinance/timesheet.html', {'form': form})
    else:
        form = TimesheetForm()
        return render(request, 'hrfinance/timesheet.html', {'form': form})

把你的观点改成这个

在模板中查询后视图.py你知道吗

{% for item in query_results %} 
<tr> 
<td>{{item.studentName }}</td> 
<td>{{item.put_field_you_have_in_model }}</td> 
</tr> 
{% endfor %} 

在列表中_时间表.html你知道吗

   {% for item in query_results %}
    <tr>
        <td>{{item.studentID}}</td>
        <td>{{item.studentName}}</td>
        <td>{{item.startDate}}</td>
        <td>{{item.endDate}}</td>
    </tr>
    {% endfor %}

相关问题 更多 >