146๐
your line raising the error is here:
comment = Comment.objects.get(pk=comment_id)
you try to access a non-existing comment.
from django.shortcuts import get_object_or_404
comment = get_object_or_404(Comment, pk=comment_id)
Instead of having an error on your server, your user will get a 404 meaning that he tries to access a non existing resource.
Ok up to here I suppose you are aware of this.
Some users (and Iโm part of them) let tabs running for long time, if users are authorized to delete data, it may happens. A 404 error may be a better error to handle a deleted resource error than sending an email to the admin.
Other users go to addresses from their history, (same if data have been deleted since it may happens).
180๐
Maybe you have no Comments record with such primary key, then you should use this code:
try:
comment = Comment.objects.get(pk=comment_id)
except Comment.DoesNotExist:
comment = None
- [Django]-Why is factory_boy superior to using the ORM directly in tests?
- [Django]-Django Rest Framework pagination extremely slow count
- [Django]-Where does pip install its packages?
- [Django]-Django MultiValueDictKeyError error, how do I deal with it
- [Django]-Django-DB-Migrations: cannot ALTER TABLE because it has pending trigger events
- [Django]-How does django handle multiple memcached servers?
18๐
You may try this way. just use a function to get your object
def get_object(self, id):
try:
return Comment.objects.get(pk=id)
except Comment.DoesNotExist:
return False
- [Django]-Django related_name for field clashes
- [Django]-How to get Request.User in Django-Rest-Framework serializer?
- [Django]-Why is factory_boy superior to using the ORM directly in tests?
0๐
I think the problem is that there is some data that has been passed in your dev server that wouldnโt be migrated to your Production server.
The easiest thing to do will be to locate those dependencies on your production database and provide them
- [Django]-Django model "doesn't declare an explicit app_label"
- [Django]-How to duplicate virtualenv
- [Django]-Pagination in Django-Rest-Framework using API-View
-2๐
comment = Comment.objects.get(pk=comment_id) if Comment.objects.filter(pk=comment_id).exists() else None
- [Django]-NumPy array is not JSON serializable
- [Django]-Django Queryset with year(date) = '2010'
- [Django]-CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False
- [Django]-What is "load url from future" in Django
- [Django]-Authenticate by IP address in Django
- [Django]-ImportError: No module named 'django.core.urlresolvers'