如果项目的预定结束根据 django 中的结束日期延迟,如何在模板中显示?

How to show in templates if a scheduled end of project is delayed based on end date in django?

我有点坚持这样一种逻辑,如果一个项目已经过了它的截止日期并且它的进度值仍然没有 100% 完成它应该显示一个 delayed 文本.我通过将 projects.project_progress 值恭敬地传递给 == 和 < 100 来传递已完成和正在进行的文本。

Models.py

class Projects(models.Model):

id=models.AutoField(primary_key=True)
project_name=models.CharField(max_length=255)
project_manager=models.ForeignKey(CustomUser,on_delete=models.CASCADE, limit_choices_to={'is_project_manager' : True})
client_name=models.ForeignKey(Clients,on_delete=models.CASCADE, null=True)
project_pic=models.ImageField(upload_to='static/website/project-images')
project_start_date=models.DateField(null=True)
project_end_date=models.DateField(null=True)
project_description=models.TextField(null=True)
project_progress = models.PositiveIntegerField(null=True, default=0,
    validators=[
        MaxValueValidator(100),
        MinValueValidator(0)
    ])
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
is_draft = models.BooleanField(default=True)
objects=models.Manager()

class Meta:
    verbose_name_plural = 'Project'

def __str__(self):
    return f'{self.project_name}'

Views.py

@login_required(login_url='user-login')

def project_details(请求,pk):

projects = Projects.objects.get(id=pk)
employed = Employee.objects.filter(project_site=projects)
invents = Project_Inventory.objects.filter(project_site=projects)

context = {

    'projects' : projects,
    'employed' : employed,
    'invents' : invents,

}

template_name ='project-admin/project-details.html'
return render(request, template_name, context) 

项目详情模板

<div class="col-md-11">
                  <div class="card">
                      <div class="card-header text-white">
                          Project Details
                      </div>
                      <div class="row p-3">
                          <div class="col-md-8">
                              <table class="table table-borderless">
                                  <thead>
                                      <span class="h4">Project Information</span class="h4">
                                      
                                      <hr>
                                  </thead>
                                  <tbody>
                                      <tr> 
                                          <th scope="row">Project Name</th>
                                          <td>{{ projects.project_name }}</td>
                                      </tr>
                                      <tr>
                                          <th scope="row">Project Manager</th>
                                          <td>{{ projects.project_manager }}</td>
                                      </tr>
                                      <tr>
                                          <th scope="row">Start Date</th>
                                          <td>{{ projects.project_start_date }}</td>
                                      </tr>
                                      <tr>
                                          <th scope="row">End Date</th>
                                          <td>{{ projects.project_end_date }}</td>
                                      </tr>
                                      <tr>
                                          <th scope="row">Description</th>
                                          <td>{{ projects.project_description }}</td>
                                      </tr>

                                      <tr>
                                          <th scope="row">Progress</th>
                                          <td><div class="progress">
                                            <div class="progress-bar" role="progressbar" style="width: {{ projects.project_progress }}%" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">{{ projects.project_progress }}%</div>
                                            <td>
                                            {% if projects.project_progress == 100 %}
                                            <p>Completed</p>
                                            {% elif projects.project_progress < 100 %}
                                            <p>Ongoing</p>
                                            {% elif projects.project_progress == 100 %}
                                            <p>Delayed</p>
                                            {% endif %}
                                            </td>
                                      </tr>
                                    
                                  </tbody>

                              </table>
                              </div>
                              <div class="col-md-4">
                              <img class="img-thumbnail" src="{{ projects.project_pic.url }}" alt="placeholder.png">
                              </div>
                        </div>

我改编了 this answer 中的代码以适合您的情况。您可以将模板中的 if 语句替换为:

{% now "Y-m-d" as todays_date %}

<td>
{% if projects.project_progress == 100 %}
   <p>Completed</p>
{% elif projects.project_progress < 100 && todays_date < project_end_date|date:"Y-m-d" %}
   <p>Ongoing</p>
{% elif projects.project_progress < 100 && todays_date > project_end_date|date:"Y-m-d" %}
   <p>Delayed</p>
{% endif %}
</td>

或者,您可以在视图中添加一个上下文变量并传递一个布尔变量,如 this answer

中所述