proper 404 exceptions

This commit is contained in:
Tomáš Mládek 2020-03-18 23:49:42 +01:00
parent 8337c6eb87
commit 67503dd7b4

View file

@ -1,6 +1,8 @@
# Create your views here. # Create your views here.
from django.contrib.syndication.views import Feed from django.contrib.syndication.views import Feed
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Count from django.db.models import Count
from django.http import Http404
from django.views.generic import TemplateView from django.views.generic import TemplateView
from sdbs_pile.pile.models import Tag, Document from sdbs_pile.pile.models import Tag, Document
@ -39,10 +41,13 @@ class TagView(BasePileView):
documents = Document.objects.all() documents = Document.objects.all()
else: else:
try: try:
tag = Tag.objects.get(id=int(name_or_id)) try:
except ValueError: tag = Tag.objects.get(id=int(name_or_id))
tag = Tag.objects.get(name=name_or_id) except ValueError:
documents = tag.documents.all() tag = Tag.objects.get(name=name_or_id)
documents = tag.documents.all()
except ObjectDoesNotExist:
raise Http404
return { return {
'tag': tag, 'tag': tag,
@ -57,8 +62,13 @@ class DocumentView(BasePileView):
def get_context_data(self, document_id: int): def get_context_data(self, document_id: int):
base_context_data = super(DocumentView, self).get_context_data() base_context_data = super(DocumentView, self).get_context_data()
try:
document = Document.objects.get(pk=document_id)
except ObjectDoesNotExist:
raise Http404
return { return {
'document': Document.objects.get(pk=document_id), 'document': document,
**base_context_data **base_context_data
} }