Use get_object_or_404 to throw 404 errors when a page's primary object isn't found, rather than a 500 error

This commit is contained in:
Michael Hall 2018-04-15 17:53:22 -04:00
parent eb5fcc9a08
commit 7db2bcf356
4 changed files with 23 additions and 29 deletions

View file

@ -3,7 +3,7 @@ from django.utils.translation import ugettext_lazy as _
from django.contrib import messages
from django.contrib.auth import logout as logout_user
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect, reverse
from django.shortcuts import render, redirect, reverse, get_object_or_404
from django.http import HttpResponse, JsonResponse
from django.utils import timezone
@ -47,7 +47,7 @@ def events_list_all(request, *args, **kwargs):
return render(request, 'get_together/events/list_events.html', context)
def show_event(request, event_id, event_slug):
event = Event.objects.get(id=event_id)
event = get_object_or_404(Event, id=event_id)
comment_form = EventCommentForm()
context = {
'team': event.team,
@ -60,7 +60,7 @@ def show_event(request, event_id, event_slug):
return render(request, 'get_together/events/show_event.html', context)
def show_series(request, series_id, series_slug):
series = EventSeries.objects.get(id=series_id)
series = get_object_or_404(EventSeries, id=series_id)
context = {
'team': series.team,
'series': series,
@ -79,7 +79,7 @@ def create_event_team_select(request):
@login_required
def create_event(request, team_id):
team = Team.objects.get(id=team_id)
team = get_object_or_404(Team, id=team_id)
if not request.user.profile.can_create_event(team):
messages.add_message(request, messages.WARNING, message=_('You can not create events for this team.'))
return redirect('show-team', team_id=team.pk)
@ -123,7 +123,7 @@ def create_event(request, team_id):
return redirect('home')
def add_event_photo(request, event_id):
event = Event.objects.get(id=event_id)
event = get_object_or_404(Event, id=event_id)
if not request.user.profile.can_edit_event(event):
messages.add_message(request, messages.WARNING, message=_('You can not make changes to this event.'))
return redirect(event.get_absolute_url())
@ -152,7 +152,7 @@ def add_event_photo(request, event_id):
return redirect('home')
def add_place_to_event(request, event_id):
event = Event.objects.get(id=event_id)
event = get_object_or_404(Event, id=event_id)
if not request.user.profile.can_edit_event(event):
messages.add_message(request, messages.WARNING, message=_('You can not make changes to this event.'))
return redirect(event.get_absolute_url())
@ -187,7 +187,7 @@ def add_place_to_event(request, event_id):
return redirect('home')
def add_place_to_series(request, series_id):
series = EventSeries.objects.get(id=series_id)
series = get_object_or_404(EventSeries, id=series_id)
if not request.user.profile.can_edit_series(series):
messages.add_message(request, messages.WARNING, message=_('You can not make changes to this event.'))
return redirect(series.get_absolute_url())
@ -219,14 +219,14 @@ def add_place_to_series(request, series_id):
return redirect('home')
def share_event(request, event_id):
event = Event.objects.get(id=event_id)
event = get_object_or_404(Event, id=event_id)
context = {
'event': event,
}
return render(request, 'get_together/events/share_event.html', context)
def edit_event(request, event_id):
event = Event.objects.get(id=event_id)
event = get_object_or_404(Event, id=event_id)
if not request.user.profile.can_edit_event(event):
messages.add_message(request, messages.WARNING, message=_('You can not make changes to this event.'))
@ -257,7 +257,7 @@ def edit_event(request, event_id):
return redirect('home')
def delete_event(request, event_id):
event = Event.objects.get(id=event_id)
event = get_object_or_404(Event, id=event_id)
if not request.user.profile.can_edit_event(event):
messages.add_message(request, messages.WARNING, message=_('You can not make changes to this event.'))
return redirect(event.get_absolute_url())
@ -288,7 +288,7 @@ def delete_event(request, event_id):
return redirect('home')
def edit_series(request, series_id):
series = EventSeries.objects.get(id=series_id)
series = get_object_or_404(EventSeries, id=series_id)
if not request.user.profile.can_edit_series(series):
messages.add_message(request, messages.WARNING, message=_('You can not make changes to this event.'))
@ -319,7 +319,7 @@ def edit_series(request, series_id):
return redirect('home')
def delete_series(request, series_id):
series = EventSeries.objects.get(id=series_id)
series = get_object_or_404(EventSeries, id=series_id)
if not request.user.profile.can_edit_series(series):
messages.add_message(request, messages.WARNING, message=_('You can not make changes to this event.'))
return redirect(series.get_absolute_url())
@ -350,7 +350,7 @@ def delete_series(request, series_id):
return redirect('home')
def show_common_event(request, event_id, event_slug):
event = CommonEvent.objects.get(id=event_id)
event = get_object_or_404(CommonEvent, id=event_id)
context = {
'org': event.organization,
'common_event': event,
@ -360,7 +360,7 @@ def show_common_event(request, event_id, event_slug):
@login_required
def create_common_event(request, org_slug):
org = Organization.objects.get(slug=org_slug)
org = get_object_or_404(Organization, slug=org_slug)
if not request.user.profile.can_create_common_event(org):
messages.add_message(request, messages.WARNING, message=_('You can not create events for this org.'))
return redirect('show-org', org_id=org.pk)
@ -389,7 +389,7 @@ def create_common_event(request, org_slug):
return redirect('home')
def share_common_event(request, event_id):
event = CommonEvent.objects.get(id=event_id)
event = get_object_or_404(CommonEvent, id=event_id)
context = {
'event': event,
}

View file

@ -2,7 +2,7 @@ from django.utils.translation import ugettext_lazy as _
from django.contrib import messages
from django.contrib.auth import logout as logout_user
from django.shortcuts import render, redirect
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse, JsonResponse
from events.models.profiles import Team, UserProfile, Member
@ -22,7 +22,7 @@ def places_list(request, *args, **kwargs):
return render(request, 'get_together/places/list_places.html', context)
def show_place(request, place_id):
place = Place.objects.get(id=place_id)
place = get_object_or_404(Place, id=place_id)
context = {
'place': place,
'event_list': Event.objects.filter(place=place).order_by('-start_time'),

View file

@ -37,7 +37,7 @@ def teams_list_all(request, *args, **kwargs):
return render(request, 'get_together/teams/list_teams.html', context)
def show_team(request, team_id, *args, **kwargs):
team = Team.objects.get(id=team_id)
team = get_object_or_404(Team, id=team_id)
upcoming_events = Event.objects.filter(team=team, end_time__gt=datetime.datetime.now()).order_by('start_time')
recent_events = Event.objects.filter(team=team, end_time__lte=datetime.datetime.now()).order_by('-start_time')[:5]
context = {
@ -76,7 +76,7 @@ def create_team(request, *args, **kwargs):
return redirect('home')
def edit_team(request, team_id):
team = Team.objects.get(id=team_id)
team = get_object_or_404(Team, id=team_id)
if not request.user.profile.can_edit_team(team):
messages.add_message(request, messages.WARNING, message=_('You can not make changes to this team.'))
return redirect('show-team', team_id=team.pk)
@ -106,7 +106,7 @@ def edit_team(request, team_id):
return redirect('home')
def delete_team(request, team_id):
team = Team.objects.get(id=team_id)
team = get_object_or_404(Team, id=team_id)
if not request.user.profile.can_edit_team(team):
messages.add_message(request, messages.WARNING, message=_('You can not make changes to this team.'))
return redirect('show-team', team_id)

View file

@ -2,7 +2,7 @@ from django.utils.translation import ugettext_lazy as _
from django.contrib import messages
from django.contrib.auth import logout as logout_user
from django.shortcuts import render, redirect
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse, JsonResponse
from django.core.exceptions import ObjectDoesNotExist
@ -30,13 +30,7 @@ def login(request):
def show_profile(request, user_id):
template = 'get_together/users/show_profile.html'
try:
user = UserProfile.objects.get(id=user_id)
except ObjectDoesNotExist:
return render(request, template, {'user': None}, status=404)
user = get_object_or_404(UserProfile, id=user_id)
teams = user.memberships.all()
@ -45,7 +39,7 @@ def show_profile(request, user_id):
'teams': teams,
}
return render(request, template, context)
return render(request, 'get_together/users/show_profile.html', context)
def edit_profile(request):