Hah! Incredibly simple! I love Django!

Thank you very much, Wayne!

On 21/06/2009, Wayne Koorts <wkoo...@gmail.com> wrote:
>
>> I have a function that looks like this:
>>
>> def getBook(request, bookID):
>>    book = Books.objects.get(id=bookID)
>>    return render_to_response('book.html', {'book': book})
>>
>> DoesNotExist at /books/8
>> Books matching query does not exist.
>>
>> How do I make it redirect to a 404 instead?
>
> There is a convenient Django shortcut for this.  If you update your
> function as follows (and add the extra import) then that should do the
> trick:
>
> from django.shortcuts import get_object_or_404
>
> def getBook(request, bookID):
>     book = get_object_or_404(Books, id=bookID)
>     return render_to_response('book.html', {'book': book})
>
> That will return a 404 if the object is not found, or continue with
> the code if it is.
>
> See this page for other Django shortcut functions:
> http://docs.djangoproject.com/en/dev/topics/http/shortcuts/
>
> Regards,
> Wayne Koorts
> http://www.wkoorts.com
>
> >
>


-- 
Magnus

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
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