Re: "Writing your first Django app" Tutorial Question

91 views
Skip to first unread message

Stefano Tranquillini

unread,
Oct 12, 2012, 8:10:31 AM10/12/12
to django...@googlegroups.com
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> wrote:
Hi, I have just started learning programming and I am following the creating a poll app tutorial at:

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.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Stefano

Jason Sibre

unread,
Oct 12, 2012, 8:50:44 AM10/12/12
to django...@googlegroups.com
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

Jan Bednařík

unread,
Oct 12, 2012, 9:16:25 AM10/12/12
to django...@googlegroups.com
Hi,

model Choice is related to model Poll by ForeignKey. All Choice
objects related to specific Poll object can by returned by:

my_poll.choice_set.all()

And that is happening in the template in for loop:

{% for choice in poll.choice_set.all %}
...

More detailed info is here:
https://docs.djangoproject.com/en/1.4/topics/db/queries/#backwards-related-objects

Honza

Tomáš Ehrlich

unread,
Oct 12, 2012, 11:39:11 AM10/12/12
to django...@googlegroups.com
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):
Reply all
Reply to author
Forward
0 new messages