I’m trying to catch an exception but does not work.This is the code in my view:
@login_required
def activities_edit(request, edit):
ActivityFormSet = modelformset_factory(Activity, can_delete=True)
act_edit= Activity.objects.filter(campaing=1).get(pk=edit)
try:
if act_edit:
if request.method == 'POST':
formset = ActivityFormSet(request.POST, request.FILES, queryset=Activity.objects.filter(pk=edit))
if formset.is_valid():
formset.save()
return HttpResponseRedirect('/activities/')
else:
formset = ActivityFormSet(queryset=Activity.objects.filter(pk=edit))
except act_edit.DoesNotExist:
return HttpResponseRedirect('/activities/')
I was also trying with: ” except act_edit.DoesNotExist: ”
but the error persists “Activity matching query does not exist.”
Any idea?
Thanks!
You need to move the statement that can cause the exception in the body of the
try:clause.The syntax is “try: something catch stuff:” your something is above the
try:Should be