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))

1 Answer 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)))
Sign up to request clarification or add additional context in comments.

8 Comments

I used it but I get this: [, , ] what does it means? (note that my Exercise model contains other fields not only name and repetitions)
yes I should have because I put some using admin interface. Anyway how can I check if I have exercises data there?
I get this: Welcome Fabrizio Avantaggiato This is your home. [, , ] (ops you changed the response wait alittle bit) I use HttpResponse because from the moment I'm learning the django/python side, I want to understand that first, I will concentrate on rendering a template after.
ok I get this: Welcome Fabrizio Avantaggiato This is your home. [{'repetitions': 30, 'name': u'bicipiti'}, {'repetitions': 10, 'name': u'tricipiti'}, {'repetitions': 0, 'name': u'panca'}] now how to isolate each value and display that in a list (without all that brackets)? is render_to_response needed?
can we use render instead? how to use it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.