0
任務是在一個模板中顯示Personal和Pensionar今天的生日,並有一個hiperlink來顯示明天的生日。Django DateField增量
當然,昨天的選擇將與明天相反。
在當前日期的情況下它工作完美,但我不知道如何增加日期變量,然後傳輸到視圖。
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>
告訴我代碼中的正確答案。