【问题标题】:AssertionError - no exception supplied - djangoAssertionError - 没有提供异常 - django
【发布时间】:2014-02-17 06:06:01
【问题描述】:

我真的很困惑,不知道它想从我这里得到什么。

这是我的简单功能

def confirm_abo(request):
  try:
      abo = Abonnement.objects.get(id=int(request.GET.get('abocid')))
      abo.status = 1
      abo.save()
      link = "http://127.0.0.1:8000/delete_link/?abocid=" + str(abo.id)
      subject = "test subject"
      message = "test message" + link
      send_mail(subject, message, 'info@test.com', [abo.email], fail_silently=False)
      return render(request,'abo_confirm.html',{'abo':abo,'abo_success':'yes'})
  except: 
      return render(request,'abo_confirm.html',{'abo_success':''})#<-- problem

我正在进入最后一行

AssertionError at /confirm_abo/ No exception supplied

错误。

我在 django1.4 和 python 2.7 中。它一直工作到现在没有任何问题..

我做错了什么?

【问题讨论】:

  • django 应该在这里提出一个更易于理解的异常消息。

标签: python django python-2.7


【解决方案1】:

检查模板abc_confirm.html是否包含无效标签用法,如下所示:

{% if x == '0' %}
...
{% else if x == '1' %}   {# used `else if` instead of `elif` %}
...
{% endif %}

可能导致AssertionError ..:

>>> from django.template import Template, Context
>>>
>>> t = Template('''
... {% if x == '0' %}
...              ..
... {% else if x == '1' %}
...              ..
... {% endif %}
... ''')
Traceback (most recent call last):
  File "<console>", line 7, in <module>
  File "/home/falsetru/.virtualenvs/django14/local/lib/python2.7/site-packages/django/template/base.py", line 125, in __init__
    self.nodelist = compile_string(template_string, origin)
  File "/home/falsetru/.virtualenvs/django14/local/lib/python2.7/site-packages/django/template/base.py", line 153, in compile_string
    return parser.parse()
  File "/home/falsetru/.virtualenvs/django14/local/lib/python2.7/site-packages/django/template/base.py", line 267, in parse
    compiled_result = compile_func(self, token)
  File "/home/falsetru/.virtualenvs/django14/local/lib/python2.7/site-packages/django/template/defaulttags.py", line 919, in do_if
    assert token.contents == 'endif'
AssertionError

【讨论】:

  • 你太棒了!这就是问题所在。很难找到为什么和在哪里......在回溯中我有django\template\defaulttags.py in do_if, line 917这一行,然后我还想,它必须在模板中......谢谢你
  • @doniyor,使用try: ... except: ... 捕获异常(未指定特定异常)使其更难找到它。指定例外或删除try: .. except: ..
  • @doniyor,恕我直言,try/except 子句套件中的 renders 均导致异常。您从 try 子句套件中发现了一个异常,但没有从 except 子句套件中发现另一个异常。
  • @doniyor,除此之外,assert .... 没有消息,原因是 no exception supplied 消息。在视图中同时尝试assert 1 == 0assert 1 == 0, "not equal"(没有try .. except ..),你就会明白我的意思了。
猜你喜欢
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-15
  • 1970-01-01
相关资源
最近更新 更多