0

I'm kinda stuck with a logic wherein if a project that is past it's due date and it's progress value is still not in 100% completion it should show a delayed text. I have passed the completed and ongoing texts just by passing the projects.project_progress value into == and < 100 respectfully.

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(request, 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) 

Project-details template

<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>

1 Answers1

1

I adapted the code from this answer to suit your situation. You can replace your if statement from the template with this:

{% 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>

Alternatively you could add a context variable in your view and pass a boolean variable like described in this answer

Andrew R.
  • 72
  • 7