在home(views.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)
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))
答案 0 :(得分:4)
我从不放弃,最后我解决了。我尝试了它,但它确实有效。
@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)))