6👍
Modify your query to use get_object_or_404
, or catch the YourModel.DoesNotExist
(3rd paragraph) exception when you’re doing the lookup, and raise a Http404
exception. When you don’t catch the DoesNotExist
exception the view raises a 500 error. As a side effect, this sends an exception email to the the ADMINS
defined it settings.py
.
Example of both cases:
from django.shortcuts import get_object_or_404
post_id = 1
post = get_object_or_404(Post, id=post_id)
# or catch the exception and do something with it
from django.http import Http404
try:
post = Post.objects.get(id=post_id)
except Post.DoesNotExist:
# id doesnt exist... do extra things here
raise Http404
1👍
The error was generated because your get
query had failed to match any record. If you want to throw a 404
page in such an event, then sdolan has already provided with you advice on how to do that. However, if you would like to assume some sensible defaults in the event that the query fails to fetch any matching records, you could wrap the call to get
around a try
and catch
block. For example:
try:
post = Post.object.get(pk=id)
except Post.DoesNotExist:
post = None
# Probably use some sensible defaults, or do something else
- [Django]-Django collectstatic on S3 uploads unchanged files everytime on Heroku
- [Django]-Xapian search terms which exceed the 245 character length: InvalidArgumentError: Term too long (> 245)
- [Django]-ImportError: No module named remote
1👍
use get_object_or_404 in query, see de documentation for more information.
http://docs.djangoproject.com/en/1.3/topics/http/shortcuts/#get-object-or-404
0👍
I guess the main problem is in your view file,where you are working with http response request object .Check that all the settings are accurate in settings.py,also use try except block for finding the error more precisely.
- [Django]-CMS or web framework a simple project
- [Django]-Django aggregate average of an annotation sum (1.6.5)
- [Django]-Django StreamingHttpResponse into a Template