我如何 return DJANGO 上 PROTECT 字段的消息错误
How do I return Message error for PROTECT Field on DJANGO
我不知道如何 return 一个关于 django 错误的简单消息,例如,我需要 return PROTECT Error on delete this OBJECT:
我的看法:
def delete_notafiscal(request, notafiscal_id):
notafiscal = NotaFiscal.objects.get(id=notafiscal_id)
context={'object':notafiscal,'forms':''}
try:
if request.method =="POST":
notafiscal.delete()
return HttpResponseRedirect(reverse("controles:notasfiscais"))
except ProtectedError as e:
print("erro",e)
return render(request,'controles/notafiscal_confirm_delete.html',context)
我的模板
<form method="post">{% csrf_token %}
<p>Você irá deletar "{{ object }}"?</p>
<input type="submit" value="Confirm">
</form>
型号
class NotaFiscal(models.Model):
nome = models.CharField(max_length=50)
documento = models.FileField(upload_to='uploads/notafiscal/')
class Item(models.Model):
id_item = models.AutoField(primary_key=True)
id_notafiscal = models.ForeignKey(NotaFiscal, on_delete=models.PROTECT, blank=True, null = True)
谢谢!
您的视图应该是这样的:
def delete_notafiscal(request, notafiscal_id):
try:
notafiscal = NotaFiscal.objects.get(id=notafiscal_id)
context={'object':notafiscal,'forms':'', 'error': ''}
if request.method =="POST":
notafiscal.delete()
return HttpResponseRedirect(reverse("controles:notasfiscais"))
# NotaFiscal will throw a DoesNotExist exception if the result does not exist
except NotaFiscal.DoesNotExist:
context['error'] = 'NotaFiscal does not exist'
except ProtectedError as e:
context['error'] ='An error occured'
return render(request,'controles/notafiscal_confirm_delete.html',context)
虽然我看不到 ProtectedError
的定义位置,但由于您没有使用 Django 表单,您可以将错误消息传递给上下文字典。
我不知道如何 return 一个关于 django 错误的简单消息,例如,我需要 return PROTECT Error on delete this OBJECT:
我的看法:
def delete_notafiscal(request, notafiscal_id):
notafiscal = NotaFiscal.objects.get(id=notafiscal_id)
context={'object':notafiscal,'forms':''}
try:
if request.method =="POST":
notafiscal.delete()
return HttpResponseRedirect(reverse("controles:notasfiscais"))
except ProtectedError as e:
print("erro",e)
return render(request,'controles/notafiscal_confirm_delete.html',context)
我的模板
<form method="post">{% csrf_token %}
<p>Você irá deletar "{{ object }}"?</p>
<input type="submit" value="Confirm">
</form>
型号
class NotaFiscal(models.Model):
nome = models.CharField(max_length=50)
documento = models.FileField(upload_to='uploads/notafiscal/')
class Item(models.Model):
id_item = models.AutoField(primary_key=True)
id_notafiscal = models.ForeignKey(NotaFiscal, on_delete=models.PROTECT, blank=True, null = True)
谢谢!
您的视图应该是这样的:
def delete_notafiscal(request, notafiscal_id):
try:
notafiscal = NotaFiscal.objects.get(id=notafiscal_id)
context={'object':notafiscal,'forms':'', 'error': ''}
if request.method =="POST":
notafiscal.delete()
return HttpResponseRedirect(reverse("controles:notasfiscais"))
# NotaFiscal will throw a DoesNotExist exception if the result does not exist
except NotaFiscal.DoesNotExist:
context['error'] = 'NotaFiscal does not exist'
except ProtectedError as e:
context['error'] ='An error occured'
return render(request,'controles/notafiscal_confirm_delete.html',context)
虽然我看不到 ProtectedError
的定义位置,但由于您没有使用 Django 表单,您可以将错误消息传递给上下文字典。