Python 为什么异常可以';不要陷入Django视图中,以及如何调试
我很满意这个删除视图:Python 为什么异常可以';不要陷入Django视图中,以及如何调试,python,django,exception,error-handling,Python,Django,Exception,Error Handling,我很满意这个删除视图: def price_unit_delete(request, pk): pu = PriceUnit.objects.get(pk=pk) template = 'includes/modal/modal_form.html' if request.POST: pu.delete() template = 'dashboard_staff/settings/includes/priceunits.html'
def price_unit_delete(request, pk):
pu = PriceUnit.objects.get(pk=pk)
template = 'includes/modal/modal_form.html'
if request.POST:
pu.delete()
template = 'dashboard_staff/settings/includes/priceunits.html'
ctx = {'action': reverse("dashboard_staff:settings_delete_priceunit", args=(pu.pk, )),
'title': 'do you really want to delete me ?',
'form': PriceUnitDeleteForm(),
'priceunits': PriceUnit.objects.all()}
return TemplateResponse(request, template, ctx)
当一个项目作为外键被引用到其他地方时,它将失败。我想用try
块捕获这个完整性错误(首先由psycopg2引起,其次由django.db本身引起)
实际上,您可以在python控制台中执行以下操作:
# in case of ```o``` being referenced somewhere else, returns False
def delete(o):
try:
o.delete()
except:
return False
我天真地想在视图中包含这样一个块,如下所示:
if request.POST:
try:
pu.delete()
except:
return JsonResponse({'data': _(
"You can't delete this object as it is used somewhere else."
)}, status=400)
但在这种情况下,不会捕获异常。我也将此视图作为CBV编写,它也无法捕获异常
我在另一个项目中尝试了这段代码,它很有效
为什么在完整性异常的情况下这不起作用?我如何调试它?
以下是我要捕获的异常的回溯:
File "/home/emilio/.virtualenvs/rdt_2.0/lib/python3.7/site-packages/django/db/backends/base/base.py", line 240, in _commit
return self.connection.commit()
psycopg2.IntegrityError: update or delete on table "prices_priceunit" violates foreign key constraint "prices_prestationtyp_default_price_unit_i_c6ec94f9_fk_prices_pr" on table "prices_prestationtype"
DETAIL: Key (id)=(1) is still referenced from table "prices_prestationtype".
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/emilio/.virtualenvs/rdt_2.0/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/emilio/.virtualenvs/rdt_2.0/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/emilio/.virtualenvs/rdt_2.0/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python3.7/contextlib.py", line 74, in inner
return func(*args, **kwds)
File "/home/emilio/.virtualenvs/rdt_2.0/lib/python3.7/site-packages/django/db/transaction.py", line 240, in __exit__
connection.commit()
File "/home/emilio/.virtualenvs/rdt_2.0/lib/python3.7/site-packages/django/db/backends/base/base.py", line 262, in commit
self._commit()
File "/home/emilio/.virtualenvs/rdt_2.0/lib/python3.7/site-packages/django/db/backends/base/base.py", line 240, in _commit
return self.connection.commit()
File "/home/emilio/.virtualenvs/rdt_2.0/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/emilio/.virtualenvs/rdt_2.0/lib/python3.7/site-packages/django/db/backends/base/base.py", line 240, in _commit
return self.connection.commit()
django.db.utils.IntegrityError: update or delete on table "prices_priceunit" violates foreign key constraint "prices_prestationtyp_default_price_unit_i_c6ec94f9_fk_prices_pr" on table "prices_prestationtype"
DETAIL: Key (id)=(1) is still referenced from table "prices_prestationtype".
查看追踪可能有助于我们帮助你。请加上。这里。查看这个帖子@NalinDobhal,这没有帮助。我知道错误的意思。这里的问题是不可能像往常一样在
try
块中处理它。最令人惊讶的是try块在python控制台中运行良好。