Hello team.
me again asking for a little help.
I created 2 tables in model.py as shown below.
class c_training(SoftDelete):
training_id = models.AutoField(primary_key=True)
training_name = models.CharField(max_length=50, null=True)
training_desc = models.CharField(max_length=150, null=True)
created_dt = models.DateField(auto_now_add=True, null=True)
last_modified_dt = models.DateField(auto_now=True, null=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='t_created_by') # add)
updated_by = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='t_updated_by') # add) , editable=False
def __str__(self):
return f"{self.training_name}"
class c_training_programed(SoftDelete):
training_prog_id = models.AutoField(primary_key=True)
training = models.ForeignKey(c_training, on_delete=models.CASCADE, related_name='c_training')
trainer = models.ForeignKey(Member, on_delete=models.CASCADE, related_name='Member')
places = models.IntegerField(null=True)
places_available = models.IntegerField(null=True)
inicial_dt = models.DateField(null=True)
final_dt = models.DateField(null=True)
total_hours = models.IntegerField(null=True)
created_dt = models.DateField(auto_now_add=True, null=True)
last_modified_dt = models.DateField(auto_now=True, null=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='tp_created_by') # add)
updated_by = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='tp_updated_by') # add) , editable=False
def __str__(self):
return f"{self.training}"
Then I created a view in views.py (as shown below) to create be able to load and see data using an html template.
def trainingProgramed(request):
trainingprgset = c_training_programed.everything.all().values()
template = loader.get_template('trainingProgramed.html')
context = {
'trainingprgset': trainingprgset,
}
#print(context)
return HttpResponse(template.render(context, request))
and finally I created an HTML file to be able to load and see data from the view above âtrainingProgramedâ
the piece of code of HTML to load the context of the view is below.
<tbody>
{% for training in trainingprgset %}
<tr>
<td>{{ training.training.training_name }}</td>
<td>{{ training.trainer.firstname }}</td>
<td>{{ training.places }}</td>
<td>{{ training.places_available }}</td>
<td>{{ training.inicial_dt }}</td>
<td>{{ training.final_dt }}</td>
<td>{{ training.total_hours }}</td>
<td>{{ training.created_by_id }}</td>
<td>{{ training.created_dt }}</td>
<td>{{ training.updated_by_id }}</td>
<td>{{ training.last_modified_dt }}</td>
<td>{{ training.is_deleted }}</td>
<td class="td-actions text-right">
<button type="button" rel="tooltip" class="btn btn-info btn-link btn-just-icon btn-sm" data-original-title="" title="">
<i class="material-icons">person</i>
</button>
<button type="button" rel="tooltip" class="btn btn-success btn-link btn-just-icon btn-sm" data-original-title="" title=""> <i class="material-icons">edit</i>
</button>
</td>
</tr>
{% endfor %}
</tbody>
The html using the context = {âtrainingprgsetâ: trainingprgset,} created in the view above to load and showing data on web browser.
for the first column {{ training.training.training_name }} I am using the field âtrainingâ from table âc_training_programedâ that is a foreign key field to table âc_trainingâ to recall the training name, but the issue is the training_name is not shown on the web broser.
On image below you can see the column Training that is referred to {{ training.training.training_name }} is not loading the training_name from c_training table.
Any advise will be really appreciated.