Hi Rick,
I'll try to explain, but it's really more about Python than Django...

(I'm not familiar with the tutorial app, so I'm basing my answers on what code 
you copied into your email.)

That import of Poll that you see in details.py doesn't even enter into it.  
It's only used on the line that says "p = get_object_or_404(...".  In fact (and 
here's where my newness in Django shows), I don't think that Poll is even 
visible (in scope) in the detail.html template.  But the Poll instance that was 
put passed to "render_to_response(..." is.  The "p" variable had an instance of 
a Poll, and it was bound to the name 'poll' in that bit "{'poll':p}".

Once in the template, it's just a matter of traversing the objects.  
poll.choice_set.all presumably returns a set (or list?) of Choice instances, 
which are bound to the variable "choice".  Then, it's just a matter resolving 
the "choice" name on that instance.  

Since Python is a dynamically typed language, so you don't need to declare the 
data type of "choice" before assigning values to it, so there's no need to do 
something like 
Choice choice = null
which you might need to do in a language like Java.  If you *did* need to do 
that, you'd have to import Choice so that the above line would work.  Since you 
*don't* need to do that, and you're never referencing "Choice" in the template, 
there's no need to import it. 

Maybe it would help to think of import as a special kind of assignment operator 
(that's a gross simplification, but it may help as a metaphor)...  Writing 
from polls.models import Choice
is a bit similar in concept to saying something like
Choice = polls.models.Choice   #make a local reference the Choice class in the 
polls.models package.

Does that help at all?  
This is all way more related to basic Python than to Django, and you may find 
it helpful to run through a Python tutorial (http://docs.python.org/tutorial/ 
for example)

Jason


On Oct 12, 2012, at 5:06 AM, Rick Chong wrote:

> Hi, I have just started learning programming and I am following the creating 
> a poll app tutorial at:
> https://docs.djangoproject.com/en/1.4/intro/tutorial03/
> 
> I hope that someone has attempted this tutorial and is able to help me on 
> some very beginner questions:
> 
> In this app, we defined 2 classes in the model - Poll & Choice:
> import datetime
> 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
>  
> 
> Then in views.py... I have the following code:
> from django.shortcuts import render_to_response, get_object_or_404
> 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})
> 
> 
> 
> Then in my template, detail.html. I have the following code:
> <h1>{{ poll.question }}</h1>
> <ul>
> {% for choice in poll.choice_set.all %}
>       <li>{{ choice.choice }}</li>
> {% endfor %}
> </ul>
> 
> 
>  
> The scripts run My question:
> 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 received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/django-users/-/2vqJuWiYVuIJ.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to