I'm trying to figure out how to label my Django form fields- at the moment I'm unable to change them. I have tried amending the field names and adding labels within models.py, but it throws an error, I'm not sure where to add them.
models.py:
from django.db import models
from django.contrib.auth.models import User
class Stats(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField(auto_now=True)
weight = models.DecimalField(max_digits=5, decimal_places=2)
run_distance = models.IntegerField(default=5)
run_time = models.TimeField()
class Meta:
db_table = 'health_stats'
ordering = ['-date']
def __str__(self):
return f"You currently weigh {self.weight}, {self.user}"
views.py:
class UpdateHealth(View):
def get(self, request, *args, **kwargs):
stats = Stats
update_form = StatUpdateForm
context = {
'stats': stats,
"update_form": update_form,
'user': stats.user,
'weight': stats.weight,
'date': stats.date,
}
return render(request, 'health_hub_update.html', context)
def post(self, request, *args, **kwargs):
stats = Stats
update_form = StatUpdateForm(data=request.POST)
context = {
'stats': stats,
"update_form": update_form,
'user': stats.user,
'weight': stats.weight,
'date': stats.date,
'run time': stats.run_time,
'run distance': stats.run_distance
}
if update_form.is_valid():
update_form.save()
return render(request, 'health_hub_update.html', context)
forms.py:
class StatUpdateForm(forms.ModelForm):
class Meta:
model = Stats
fields = ('user', 'weight', 'run_distance', 'run_time')
Any help would be appreciated!