2

In home (views.py) how to display a list of all exercises (name and repetitions) linked to the same user Card?

models.py

class Card(models.Model):
    user = models.OneToOneField(User)
    trainer = models.ForeignKey(User, related_name='trainer')
    def __unicode__(self):
        return u'%s' % (self.id)




class Exercise(models.Model):
    card = models.ForeignKey(Card)
    name = models.CharField(max_length=50)
    repetitions = models.IntegerField(default=0, blank=True, null=True)

    def __unicode__(self):
        return u'%s' % (self.name)

views.py

from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from cards.models import Card, Exercise

@login_required
def home(request):
    user = request.user.get_full_name()      
    return HttpResponse("Welcome %s This is your home." % (user))
4

1 回答 1

4

I never give up and finally I fix it. I tried it and it works.

@login_required
def home(request):
    user = request.user.get_full_name() 

    exercises = Exercise.object.filter(card__user=request.user)

    html = []
    for k in exercises:
        html.append('<tr><td>%s - %s</td></tr>' % (k.name, k.repetitions))
    return HttpResponse('Welcome %s This is your home.<br/><table>%s</table>' % (user, '\n'.join(html)))
于 2013-03-30T09:11:29.987 回答