Executive Summary:
I need to know how to consistently write fbv’s that get the content I want
out of my database and into my templates for display on my site in the way
I want it displayed.
I confess, I don’t get it. Mark me down in points for asking a dumb
question. I don’t care. I need help.
Problem 1
I have successfully constructed my own function based view for listing all
my articles with all their sections in a table of contents. At first they
were not in order. I put an orderby on the models meta but that made no
difference. Then I put an order_by ID in the view and that worked – except
they were descending instead of ascending. Then I put reversed on the
template and it worked.
However, in the course of struggling with Problem 2, suddenly my table of
contents is out of order again, and I* have not touched* those lines of
code. I don’t understand how that could be. Daniel Roseman suggested I
create a new field, number it, and order by that instead of relying on the
ID as PK, and I will do that if I have to, but right after that is when I
discovered reversed so I didn’t do it then. Now I’d like to know why the
order changed when I haven’t touched the code that (supposedly) fixed it.
Template for loop code:
{% for a in A %}
{% if a.sections_set.all %}
<h1><li>{{ a.name }} {{ a.popular_name }}</li></h1>
{% for section in a.sections_set.all reversed %}
Model meta:
class meta:
ordering = ['id']
View:
def TOC(request):
A = Articles.objects.order_by('id')
for a in A:
a.sections_set.order_by('id')
return render(request, 'statute.html', {'A': A,})
Problem 2 (the bigger one)
I cannot for the life of me get any articles or sections to come up on
their own detail page. If I mouse over the links in my table of contents,
the little preview url in the bottom right corner comes up correct for each
one. I have bounced around from name error to type error to no match errors
to does not exist errors and back again.
I have read the documentation. I have done the polls tutorial. I have read
about context. None of that matters. I have researched, tried this that
and the other thing, and gotten nowhere. I have no doubt that it is
something simple but simple or not I’m not getting it. If you can show me
how to get it right, and keep getting it right consistently as I go
forward, I would be very grateful.
Yes, I know there are class based views. I read that I can subclass them.
But I fail to see the advantage of subclassing something I understand even
less over learning to write what I want directly. I can’t learn without
understanding, and to do that I need to get my hands dirty writing the
code, not relying on someone else’s magic. I am not interested in a flame
war as to whether or not CBV’s were a good idea, but I will point out that
there is almost zero documentation or tutorials on how to write your own
FBV’s, and certainly nothing on any complex FBV. I’ve been searching for
over a week for it.
The template I am calling does come up. It overwrites base in all the
places I expect it to. But there is no content. Debug toolbar says it is
the right view. The only sql queries are sessions and user. Shouldn’t there
be at least one for the content? The context processors say my GET
querydict is empty and my content is an empty string. The only fields I
want to show on the detail pages are the name and the text. Here is the
relevant portion of my template:
{% block content %}
<div>
{{ text }}
</div>
{% endblock content %}
(I tried {{a.text}} earlier, that didn’t work either.)
Here is my urls.py
urlpatterns = patterns('',
url(r'^$', views.TOC, name='TOC'),
url(r'^(?P<id>\d+)\/(?P<slug>[-\w]+)', views.articleView, name='articleView'
),
url(r'^(?P<id>\d+)\/(?P<slug>[-\w]+)', views.sectionView, name='sectionView'
),
)
Here is my *most recent* attempt at a view:
def articleView(request, **kwargs):
a = Articles.objects.values('name', 'text')
context = {'a' : a}
return render(request, 'statute2.html', context)
Note that this is *exactly *the same format as laid out in Polls Tutorial
3: https://docs.djangoproject.com/en/1.7/intro/tutorial03/#a-shortcut-render
The only difference is the presence of **kwargs. I was getting all kinds of
errors before I tried **kwargs, and I was getting all kinds of errors with
GET before I tried values(). Now I just have a blank template, which I also
had at various times before **kwargs and values().
--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/12c042f6-38f2-4b40-94b7-bf6e9d859879%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.