From: Rick Chong <ulian...@gmail.com>
Date: Fri, 12 Oct 2012 03:06:17 -0700 (PDT)
Local: Fri, Oct 12 2012 6:06 am
Subject: "Writing your first Django app" Tutorial Question
Hi, I have just started learning programming and I am following the I hope that someone has attempted this tutorial and is able to help me on In this app, we defined 2 classes in the model - Poll & Choice: > import datetime Then in views.py... I have the following code: > from django.utils import timezone > from django.db import models > # Create your models here. > class Poll(models.Model): > question = models.CharField(max_length=200) > pub_date = models.DateTimeField('date published') > def __unicode__(self): > return self.question > def was_published_recently(self): > return self.pub_date >= timezone.now() - datetime.timedelta(days=1) > was_published_recently.admin_order_field = 'pub_date' > was_published_recently.boolean = True > was_published_recently.short_description = 'Published recently?' >> class Choice(models.Model): > poll = models.ForeignKey(Poll) > choice = models.CharField(max_length=200) > votes = models.IntegerField() > def __unicode__(self): > return self.choice > from django.shortcuts import render_to_response, get_object_or_404 Then in my template, detail.html. I have the following code: > from polls.models import Poll >> def detail(request, poll_id): > p = get_object_or_404(Poll, pk=poll_id) > return render_to_response('polls/detail.html', {'poll': p}) > <h1>{{ poll.question }}</h1> The scripts run My question: > <ul> > {% for choice in poll.choice_set.all %} > <li>{{ choice.choice }}</li> > {% endfor %} > </ul> In views.py, I only import Poll class from model... why am I able to display choice, which is a variable under Choice class? Thank you very much. Rick You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||