我正在创建时间表应用程序。我想每周显示时间表,这是我能够做到的。 我已经创建了上周和下周按钮。
我应该使用哪种方法,以便当有人单击前一周时,它显示前一周的timsheet和下周的timsheet。
注意-我有一个时间表表,其中每天包含一个条目用于时间输入。
models.py-
class TimeRecords(models.Model):
emp_id = models.IntegerField(verbose_name = 'Employee ID')
ts_date = models.DateField(null=False, blank=False,verbose_name = 'Time Sheet Date')
ts_effort = models.PositiveIntegerField(default=8,verbose_name = 'Hours worked')
ts_desc = models.CharField(max_length=200,verbose_name = 'Time Description')
class Meta:
verbose_name = 'Time Records'
verbose_name_plural = 'Time Records'
unique_together = (("emp_id", "ts_date"),)
ordering = ["-emp_id"]
def __str__(self):
return self.ts_desc
forms.py ---
class CreateTimeSheetForm(forms.ModelForm):
emp_id = forms.IntegerField(widget=forms.HiddenInput(), initial=123)
class Meta:
model = TimeRecords
fields = '__all__'
labels = {
"ts_desc": "Task Description"
}
widgets = {
'ts_date': forms.TextInput(attrs={'readonly': True}),
}
views.py ---
def dateEnds():
date=datetime.date.today()
year, week, dow = date.isocalendar()
if dow == 1:
start_date = date
else:
start_date = date - timedelta(dow)
end_date = start_date + datetime.timedelta(days=7)
return start_date ,end_date
def daterange():
start_date, end_date=dateEnds()
for n in range(int((end_date - start_date).days)):
yield start_date + timedelta(n)
def timeEntryList(request):
start_date, end_date=dateEnds()
time_records=TimeRecords.objects.filter(emp_id=emp_id,ts_date__range=
(start_date, end_date))
context = {'time_records': time_records}
return render(request, 'timesheets/list.html', context)
list.html-
<tr> <th > Date</th> <th> Efforts</th> <th> Description</th> <th> Action</th> </tr>
{% for time_record in time_records %}
<tr>
<td >{{ time_record.ts_date|date:"SHORT_DATE_FORMAT" }}</td>
<td>{{ time_record.ts_effort }}</td>
<td>{{ time_record.ts_desc }}</td>
<td ><a href="../{{time_record.id}}/edit">Edit</a> <a href="../{{time_record.id}}/delete/">Delete</a></td>
</tr>
{% endfor %}
</table>
<a href="/timesheets/view" ><button type="button" class="btn" >Previous Week</button></a>
答案 0 :(得分:0)
这就是我要解决这个问题的方式。
在models.py
中,
class TimeRecord(models.Model):
ts_date = models.DateTimeField()
# Other fields
def get_week(self):
return self.ts_date.isocalendar()[1]
在views.py
中,我将使用列表推导来获取上周和下周的时间记录。
from django.utils import timezone
def my_view(request):
context = dict()
time_records = TimeRecord.objects.all()
current_week = timezone.now().isocalendar()[1]
current_records = [time_record for time_record in time_records if time_record.get_week() == current_week]
if request.method == 'POST':
week = request.POST['week']
if week == 'next-week':
current_records = [time_record for time_record in time_records if
time_record.get_week() == current_week + 1]
if week == 'last-week':
current_records = [time_record for time_record in time_records if
time_record.get_week() == current_week - 1]
context['current_records'] = current_records
return render(request, 'my.html', context)
最后,在模板中,我将在下周和上周使用隐藏的表单输入。
<div>
{% for record in current_records %}
<p># {{ forloop.counter }} {{ record.ts_date }}</p>
{% endfor %}
<form action="" method="post">
{% csrf_token %}
<input type="hidden" name="week" value="next-week">
<button type="submit">next week</button>
</form>
<form action="" method="post">
{% csrf_token %}
<input type="hidden" name="week" value="last-week">
<button type="submit">last week</button>
</form>
</div>