Hi Stefano,
your understanding is incorrect. Django (neither Python) doesn't load (or 
import) anything by himself. Everything what you import is everything what 
you get. There are few exceptions, like Python builtin module (and default 
tags/templates in Django templates), but that's definitely not this case.

Template context consist of:
 - your context dictionary (in this case {'poll': p})
 - dictionaries of all context processors (if you use RequestContext)
 - and default tags/filters

One don't have to load Choice class, because choice instances are returned 
with {% for choice in poll.choice_set.all %}. You can access Choice class 
in choice.__class__ if you want, but that's unnecessary in most cases (in 
templates).

I'm writing this because recently I've been working with PHP and Yii 
framework and that kind of autoloading, you've mentioned, *does* occur 
there. It doesn't work like that in Python (and Django doesn't add it).

Cheers,
 Tom

Dne pátek, 12. října 2012 14:12:10 UTC+2 Stefano Tranquillini napsal(a):
>
> Not an expert at all, but this is my understanding.
> In the view you only set up data that will be available in the template, 
> there's no relation between the imports there and what can be displayed in 
> the template. 
> the templates probably loads the classes when he need to display something.
> not sure.
>
>  
>
> On Fri, Oct 12, 2012 at 12:06 PM, Rick Chong <ulia...@gmail.com<javascript:>
> > 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...@googlegroups.com<javascript:>
>> .
>> To unsubscribe from this group, send email to 
>> django-users...@googlegroups.com <javascript:>.
>> For more options, visit this group at 
>> http://groups.google.com/group/django-users?hl=en.
>>
>
>
>
> -- 
> Stefano
>  

-- 
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/-/SDIG4V7RbnwJ.
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