views:

454

answers:

3

For my first Django app, I'm trying to write a simple quote collection site (think bash.org), with really simple functionality, just to get my feet wet. I'm using sqlite as my database, since it's the easiest to setup.

Here's my only model right now:

class Quote(models.Model):
    text = models.TextField();
    upvotes = models.IntegerField(default=0)
    downvotes = models.IntegerField(default=0)
    active = models.BooleanField(default=False)
    date_published = models.DateTimeField(auto_now_add=True)

And a really simple detail template, just to dump the information:

Quote: {{ quote.text }}<br>
Upvotes: {{ quote.upvotes }}<br>
Downvotes: {{ quote.downvotes }}<br>
Published: {{ qoute.date_published|date:"F j, Y, g:i a" }}

When I go to the detail page, everything for the given object is outputted properly, except for the datetime (blank). However, I've checked the database and verified that there is a datetime stored in that object's column, and it shows up fine admin area. Also, when I run the shell from manage.py, here's what I get:

>>> q = Quote.objects.all()[0]
>>> q.date_published
datetime.datetime(2009, 7, 24, 23, 1, 7, 858688)

Also, I'm using the generic view django.views.generic.list_detail.object_detail to handle the request, but I also tried to use the view below and got the same result.

def detail(Request, id):
    q = get_object_or_404(Quote, pk=id)
    return render_to_response('quotable/quote_detail.html', {'quote': q})

Am I'm doing something wrong in my attempt to display the date, or is something else going on here?

Thanks.

A: 

I'm not sure what you're doing with that date: filter -- what happens if you replace it with something simple, such as date:"D d M Y?

Alex Martelli
+2  A: 

As Adam Bernier mentioned, you're misspelling quote

Steve Losh
this is one of the reasons I so despise django templates. I don't want a system that fails silently. I want it to bitch at me for every thing I get wrong. Even if isn't always wrong, but it thinks it COULD be wrong, I want to know about it, so that I can go in and put some extra plating on it so that the next person to look at it can see that though it looks like it might be wrong, I've already thought of that, commented on why I think it's actually right, and told the program the same.
TokenMacGuy
when i did something like that, it told me the line that it failed on...?
geowa4
**@TokenMacGuy** The "template variables can fail silently" is a pretty contentious issue among Django folk. One point of view is the one you mentioned, another is "small templating errors (possibly by non-programmers, who the template system is geared toward) should not bring down an entire page of the site." I personally think there should be a setting you can change to be warned of silent errors (or maybe tie it to the existing `DEBUG` setting).
Steve Losh
@Steve Losh: there is a setting that does just that: TEMPLATE_STRING_IF_INVALID
piquadrat
piquadrat: That doesn't cause errors though, just put a string in place of invalid variables, isn't it?
uswaretech
**@piquadrat** Wow, I didn't know about that. Thanks! The documentation *does* say it can cause lots of problems in the admin though, so it's not a set-it-and-forget-it kind of thing: <http://docs.djangoproject.com/en/dev/ref/templates/api/#invalid-template-variables>
Steve Losh
@uswaretech: You can make it throw an exception when it fails: http://www.djangosnippets.org/snippets/646/
Emil Stenström
A: 

I believe that django.views.generic.list_detail.object_detail uses a variable named object_id, not id.

    urlpatterns = patterns('',
        (r'^$', 'django.views.generic.list_detail.object_list', info_dict),
        (r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
        url(r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='polls/results.html'), 'poll_results'),
        (r'^(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'),

)

When you change to using the detail template, your url pattern will be wrong.

hughdbrown