任务是在一个模板中显示今天出生日的Personal和Pensionar,并有一个hiperlink来显示明天出生日的情况。
当然,昨天选项将与明天相反。
如果是当前日期,它工作正常,但我不知道如何增加日期变量,然后传输到视图。
model.py
class Personal(models.Model):
nume = models.CharField(max_length=40)
prenume = models.CharField(max_length=40)
dataNastere = models.DateField(blank=True, null=True)
def __str__(self):
return self.nume
class Pensionar(models.Model):
nume = models.CharField(max_length=40)
prenume = models.CharField(max_length=40)
dataNastere = models.DateField(blank=True, null=True)
def __str__(self):
return self.nume
url.py
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^omagiatulZilei/$', views.omagiatulZilei_list, name='omagiatulZilei_list'),
url(r'^omagiatulZilei_next/$', views.nextDate, name='nextDate'),
views.py
def omagiatulZilei_list(request):
today = timezone.now().date()
personals = Personal.objects.order_by('nume').filter(
dataNastere__month=today.month,
dataNastere__day=today.day)
pensionars = Pensionar.objects.order_by('nume').filter(
dataNastere__month=today.month,
dataNastere__day=today.day)
context = {'personals' : personals, 'pensionars' : pensionars}
return render(request, 'blog/omagiatulZilei_list.html', context=context)
def nextDate(request, ??): # ?? is a datefield variable
nextD = ?? + 1 # increment by 1 (the meaning is next day)
personals = Personal.objects.order_by('nume').filter(
dataNastere__month=nextD.month,
dataNastere__day=nextD.day)
pensionars = Pensionar.objects.order_by('nume').filter(
dataNastere__month=nextD.month,
dataNastere__day=nextD.day)
context = {'personals' : personals, 'pensionars' : pensionars}
return render(request, 'blog/omagiatulZilei_list.html', context=context)
omagiatulZilei_list.html
{% load staticfiles %}
<html>
<head>
<title>Omagiatul Zilei</title>
</head>
<body>
<a href="{% url 'nextDate' %}">next date</a><br>
{% for personal in personals %}
{{ personal.nume }}
{{ personal.prenume }},
{{ personal.dataNastere }} <br>
{% endfor %}
{% for pensionar in pensionars %}
{{ pensionar.nume }}
{{ pensionar.prenume }},
{{ pensionar.dataNastere }} <br>
{% endfor %}
</body>
</html>
在代码中告诉我正确答案。
答案 0 :(得分:1)
today = timezone.now().date() # gets today
nextD = today + datetime.timedelta(days=1) # adds one day to today