What's the best way to handle Django's objects.get?

Whenever I do this:

thepost = Content.objects.get(name="test")

It always throws an error when nothing is found. How do I handle it?

Answers


from django.core.exceptions import ObjectDoesNotExist

try:
    thepost = Content.objects.get(name="test")
except ObjectDoesNotExist:
    thepost = None

I'm writing this from memory, so I'm not sure about the exception.


Need Your Help

Castor XML Mapping same name with the child node

java xml-parsing castor

I am facing a problem with castor while trying to parse an xml which contains same name for the parent and child node... Here are the details:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.