我希望有一个待办事项清单,其中有一个名为“ projekt”的模型,并且我想根据timeuntil标签为html输出着色(如果少于7天,则应具有bootstrap类“ danger”)如果超过7天,则应该设置“成功”课程
这是我的模型。py:
class Project(models.Model):
name = models.CharField(max_length=100)
active = models.BooleanField(default=True)
beginning_project = models.DateField()
beginning_project_time = models.TimeField(blank=True, null=True)
deadline = models.DateField()
deadline_time = models.TimeField(blank=True, null=True)
beschreibung = models.TextField()
def __str__(self):
return self.nam
这是我的views.py:
def Home(request):
proj = Project.objects.filter(active=True)
context = {
'proj':proj
}
return render(request, "home.html", context)
最后是我的模板:
home.html:
{% for proj in proj %}
<div class="row mx-auto text-center">
<div class="col-12 projcard">
<a href="{% url 'apps:Project' %}">
<p class="display-4 mx-auto">
{{ proj.name }}x
</p>
{% if proj.beginning_project|timeuntil > 7 %}
<button class="btn btn-success">
<u>
starts in {{ proj.beginning_project|timeuntil }}
</u>
</button>
{% else %}
<button class="btn btn-danger">
<u>
starts in {{ proj.beginning_project|timeuntil }}
</u>
</button>
{% endif %}
</a>
</div>
</div>
{% endfor %}
答案 0 :(得分:0)
在您的 views.py 中,您可以执行以下操作:
from datetime import datetime
def Home(request):
proj = Project.objects.filter(active=True)
today = int(datetime.now().strftime("%d"))
for p in proj:
if p.beginning_project.day - today > 7:
p.danger_mode = False
else:
p.danger_mode = True
,然后在您的 home.html 中:
{% for proj in proj %}
<div class="row mx-auto text-center">
<div class="col-12 projcard">
<a href="{% url 'apps:Project' %}">
<p class="display-4 mx-auto">
{{ proj.name }}x
</p>
<button class="btn btn-{% if proj.danger_mode %}danger{% else %}success{% endif %}">
<u>
starts in {{ proj.beginning_project }}
</u>
</button>
</a>
</div>
</div>
{% endfor %}