Greetings, sports fans.
There is an old saying, three strikes and you're out. They also say the
third time is the charm. Let’s see which one this is for me, shall we?
This is the 3rd time in the last two years that I have been stuck for more
than a few *days* on a problem getting my data onto a template. (I’m
obviously not a full time developer). Although the symptom is the same,
they do not all seem to be precisely the same cause, but I don’t think that
matters. What matters here is that clearly, I just plain don’t understand
context. It seems like a simple concept, but I can’t seem to execute it
*routinely
and effectively*.
Here’s my view:
#!/usr/bin/python
#-*- coding: utf-8 -*-
from django.shortcuts import render
from .models import Jurisdiction
def statehome(request, twodigit):
state = Jurisdiction.objects.all()
ace = 'spades'
context={'state':state, 'ace':ace}
return render (request, 'statehome.html', context)
Twodigit is just the postal code that I use in the url. Nothing related to
state or jurisdiction displays on the template. I put ‘ace’ in there just
as a test, and ‘spades’ shows up bright and bold just like it should. Here
is the relevant portion of the template:
<h2 class="title">{{state.name}}{{ ace }}</h2>
this is what I get in the shell:
In [23]: J=Jurisdiction.objects.all()
In [27]: for state in J:
...: print(state.name)
...:
United States of America
Alabama
Alaska
Arizona
...etc…
It is not called for in this particular template, but I decided to put a
for loop in there, too, just to test it:
{% for state in states %}
{{ state.name }}
{% endfor %}
You guessed it! Nothing.
According to Django Debug Toolbar, I have this context in my template:
{'ace': 'spades', 'state': '<<queryset of bench.Jurisdiction>>'}
But that same Toolbar also says I only executed two sql queries: one for
the session and one for the user.
Earlier, I discovered that *a queryset of one is not the same as a single
objec*t, and that it always helps to get the right name of your object:
In [3]: Jurisdiction.objects.filter(name="United States")
Out[3]: <QuerySet []>
In [5]: Jurisdiction.objects.get(name="United States")
DoesNotExist: Jurisdiction matching query does not exist.
In [6]: Jurisdiction.objects.get(name="United States of America")
Out[6]: <Jurisdiction: United States>
In [7]: us=Jurisdiction.objects.get(name="United States of America")
In [8]: us.name
Out[8]: 'United States of America'
So when I used ‘get’ instead of ‘filter’ in my view, it worked. But that
does *not* solve my problem.* What happens when I want a list of objects? **Or
several different objects and their attributes? Isn’t that supposed to be a
queryset? Then how do I make them show up in the template? Why does my for
loop and state.name work in the shell with objects.all(), but neither works
in the template?*
I don’t understand. I’m sure it is staring me right in the face, and is
obvious to you, but *I just plain don’t get it*. So if I could get an
explanation, and a how to, that would be great. And if you can point to a
book, website, tutorial, whatever – not the official docs, thank you, I can
recite them in my sleep and clearly I’m not getting it – that explains all
this slowly, step by step, big picture and little picture, with examples
and an explanation of vocabulary, that would be really great. Thanks.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/11932523-c612-4938-bb27-0bd1527e3ed8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.