I have been building a project for the first time ever! I am super excited however, I am running into an issue. Which I am not experienced enough to troubleshoot on my own right now.
I have an “Award” model and a “Status” model. The Status is linked to the Award model so users can make a comment on the award and it records the date, the user who made the comment and what the comment says. On the admin page, I created 2 awards and 2 comments, both comments are linked to award #1. When looking at the template (html) it is showing the comments for both awards. Additionally, I would like to just display the most recent comment on the main table, allowing the user to click a link for more info and a list of all the comments for that award.
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
UIC_CHOICES = [
('other', 'Other')
]
RANK_CHOICES = [
(
'ENLISTED',
(
('E1','PVT'),
('E2','PV2'),
('E3','PFC'),
('E4','SPC'),
('E4-3', 'CPL'),
('E5', 'SGT'),
('E6', 'SSG'),
('E7', 'SFC'),
('E8', 'MSG'),
('E8-2', '1SG'),
('E9', 'SGM'),
('E9-2', 'CSM'),
('E9-3', 'SMA'),
),
),
(
'WARRANT',
(
('WO1', 'WO1'),
('CW2', 'CW2'),
('CW3', 'CW3'),
('CW4', 'CW4'),
('CW5', 'CW5'),
),
),
(
'OFFICER',
(
('2LT', '2LT'),
('1LT', '1LT'),
('CPT', 'CPT'),
('MAJ', 'MAJ'),
('LTC', 'LTC'),
('COL', 'COL'),
('BG', 'BG'),
('MG', 'MG'),
('LTG', 'LTG'),
('GEN', 'GEN'),
('GA', 'GA'),
),
),
('other', 'Other')
]
RECOMMENDED_AWARD = [
('AAM', 'AAM'),
('ARCOM', 'ARCOM'),
('MSM', 'MSM'),
('LOM', 'LOM'),
('OTHER', 'Other')
]
AWARD_REASON = [
('ACH', 'ACH'),
('SVC', 'SVC'),
('PCS', 'PCS'),
('ETS', 'ETS'),
('RET', 'RET'),
('HER', 'HEROISM'),
('VAL', 'VALOR'),
('OTH', 'Other'),
]
class Award(models.Model):
date_created = models.DateTimeField(auto_now_add=True)
date_modified = models.DateTimeField(auto_now=True)
first_name = models.CharField(max_length=50, verbose_name = "First Name")
last_name = models.CharField(max_length=50, verbose_name = "Last Name")
rank = models.CharField(max_length=5, default=None,
choices=RANK_CHOICES)
unit = models.CharField(max_length=6, default=None,
choices=UIC_CHOICES)
recommended_award = models.CharField(max_length=5, default=None,
choices=RECOMMENDED_AWARD, verbose_name = "Recommended Award")
award_reason = models.CharField(max_length=3, default=None,
choices=AWARD_REASON, verbose_name = "Award Reason")
date_received = models.DateField(blank=True, null=True, default=None,
verbose_name = "Date Received")
presentation_date = models.DateField(blank=True, null=True, default=None,
verbose_name = "Presentation Date")
letter_of_lateness = models.BooleanField(blank=True, null=True,
default=None, verbose_name = "Letter of Lateness Required?")
date_sent_higher = models.DateField(blank=True, null=True, default=None,
verbose_name = "Date Submitted to Higher")
submitted_by = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
def __str__(self):
return self.last_name + ', ' + self.first_name + ' ' + str(self.date_created)[:10]
class Status(models.Model):
date_created = models.DateTimeField(auto_now_add=True)
date_modified = models.DateTimeField(auto_now=True)
status_comment = models.TextField(max_length=200)
award_link = models.ForeignKey(Award, null=False,
on_delete=models.DO_NOTHING)
submitted_by = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
class Meta:
verbose_name_plural = "Statuses"
def __str__(self):
return self.status_comment
views.py
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
from .forms import RegisterForm
from .models import Award
from .models import Status
# Create your views here.
def home(request):
awards = Award.objects.all()
status = Status.objects.all()
context = {
'awards':awards,
'status':status,
}
#Check if logging in
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
# Authenticate
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
messages.success(request, "Login Successful.")
return redirect('home')
else:
messages.error(request, "ERROR: Login unsuccessful. Try again or contact site administrator.")
return redirect('home')
else:
return render(request, 'home.html', context)
def logout_user(request):
logout(request)
messages.success(request, "You have been successfully logged out.")
return redirect('home')
def register_user(request):
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
form.save()
# Auth and Login
username = form.cleaned_data['username']
password = form.cleaned_data['password1']
user = authenticate(username=username, password=password)
login(request, user)
messages.success(request, "You have successfully registered.")
return redirect('home')
else:
form = RegisterForm()
return render(request, 'register.html', {'form':form})
return render(request, 'register.html', {'form':form})
award_table.html
{% if user.is_authenticated %}
<div class="container">
{% if not awards.all %}
<h1>No Awards Present.</h1>
{% else %}
<h1>Awards</h1>
<table class="table table-hover text-center">
<thead class="text-center text-wrap">
<tr>
<th scope="col">Unit</th>
<th scope="col">Rank</th>
<th scope="col">Name</th>
<th scope="col">Award Type</th>
<th scope="col">Reason</th>
<th scope="col">Date Received</th>
<th scope="col">Presentation Date</th>
<th scope="col">LoL Required</th>
<th scope="col">Date to Higher</th>
<th scope="col">Status Date</th>
<th scope="col">Current Status</th>
<th scope="col">User</th>
</tr>
</thead>
<tbody>
{% if awards %}
{% if status %}
{% for stat in status %}
{% for award in awards %}
<tr>
<td style="width: 8rem;">{{ award.unit }}</td>
<td style="width: 5rem;">{{ award.rank }}</td>
<td><a href="http://www.google.com">{{ award.last_name|title }}, {{ award.first_name|title }}</a></td>
<td>{{ award.recommended_award }}</td>
<td>{{ award.award_reason }}</td>
{% if award.date_received == None %}
<td>N/A</td>
{% else %}
<td>{{ award.date_received }}</td>
{% endif %}
{% if award.presentation_date == None %}
<td>N/A</td>
{% else %}
<td>{{ award.presentation_date }}</td>
{% endif %}
{% if award.letter_of_lateness == None %}
<td>N/A</td>
{% else %}
<td>{{ award.letter_of_lateness }}</td>
{% endif %}
{% if award.date_sent_higher == None %}
<td>N/A</td>
{% else %}
<td>{{ award.date_sent_higher }}</td>
{% endif %}
<td style="width: 5rem;">{{ stat.date_modified }}</td>
<td class="text-start">{{ stat.status_comment }}</td>
<td class="text-start">{{ stat.submitted_by|title }}</td>
{% endfor %}
{% endfor %}
{% endif %}
{% endif %}
</tbody>
</table>
{% endif %}
</div>
{% endif %}```