【问题标题】:Django error while template rendering: NoReverseMatch found模板渲染时出现 Django 错误:发现 NoReverseMatch
【发布时间】:2013-08-29 06:46:34
【问题描述】:

以下代码引发错误。我搜索了解决方案,但找不到与我的方案有关的任何内容。任何帮助将不胜感激。

#urls.py
url(r'^password/reset/confirm/(?P<uidb36>[0-9A-Za-z]+)\/(?P<token>.+)/$', 
    app_name.password_reset_confirm,name='auth_password_reset_confirm'),

我需要在我的视图中将电子邮件模板呈现为字符串。

#views.py
from django.template import Context, loader
t = loader.get_template('app_name/email.html')
message = t.render(Context({"domain":"foo.com","protocol":"https"}))
user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)

以下是 html 文件,简化为 SO:

{% load i18n %}{% autoescape off %}
<html>
    <body>
        {% block reset_link %}<br />
            <a href="{{ protocol }}://{{ domain }}/accounts/password/reset/confirm/{{uid}}/{{token}}">{{ protocol }}://{{ domain }}/accounts/password/reset/confirm/{{uid}}/{{token}}</a>
            <br />
        {% endblock %}
        {% trans "Thanks for using our product!" %}<br /><br />
        {% blocktrans %}Regards, <br />{{ site_name }} team{% endblocktrans %}
    </body>
</html>
{% endautoescape %}

错误回溯:

NoReverseMatch                            Traceback (most recent call last)
/usr/local/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 message = t.render(Context({"domain":"foo.com","protocol":"https"}))

/usr/local/lib/python2.7/dist-packages/django/template/base.pyc in render(self, context)
    138         context.render_context.push()
    139         try:
--> 140             return self._render(context)
    141         finally:
    142             context.render_context.pop()

/usr/local/lib/python2.7/dist-packages/django/template/base.pyc in _render(self, context)
    132 
    133     def _render(self, context):
--> 134         return self.nodelist.render(context)
    135 
    136     def render(self, context):

/usr/local/lib/python2.7/dist-packages/django/template/base.pyc in render(self, context)
    828         for node in self:
    829             if isinstance(node, Node):
--> 830                 bit = self.render_node(node, context)
    831             else:
    832                 bit = node

/usr/local/lib/python2.7/dist-packages/django/template/debug.pyc in render_node(self, node, context)
     72     def render_node(self, node, context):
     73         try:
---> 74             return node.render(context)
     75         except Exception as e:
     76             if not hasattr(e, 'django_template_source'):

/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.pyc in render(self, context)
     31         old_setting = context.autoescape
     32         context.autoescape = self.setting
---> 33         output = self.nodelist.render(context)
     34         context.autoescape = old_setting
     35         if self.setting:

/usr/local/lib/python2.7/dist-packages/django/template/base.pyc in render(self, context)
    828         for node in self:
    829             if isinstance(node, Node):
--> 830                 bit = self.render_node(node, context)
    831             else:
    832                 bit = node

/usr/local/lib/python2.7/dist-packages/django/template/debug.pyc in render_node(self, node, context)
     72     def render_node(self, node, context):
     73         try:
---> 74             return node.render(context)
     75         except Exception as e:
     76             if not hasattr(e, 'django_template_source'):

/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.pyc in render(self, context)
     52         if block_context is None:
     53             context['block'] = self
---> 54             result = self.nodelist.render(context)
     55         else:
     56             push = block = block_context.pop(self.name)

/usr/local/lib/python2.7/dist-packages/django/template/base.pyc in render(self, context)
    828         for node in self:
    829             if isinstance(node, Node):
--> 830                 bit = self.render_node(node, context)
    831             else:
    832                 bit = node

 /usr/local/lib/python2.7/dist-packages/django/template/debug.pyc in render_node(self, node, context)
     72     def render_node(self, node, context):
     73         try:
---> 74             return node.render(context)
     75         except Exception as e:
     76             if not hasattr(e, 'django_template_source'):

/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.pyc in render(self, context)
    422                         # the path relative to the project. This makes a

    423                         # better error message.

--> 424                         raise e
    425             else:
    426                 if self.asvar is None:

NoReverseMatch: Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb36': '', u'token': ''}' not found.

【问题讨论】:

  • 你能告诉我们urls.py吗?当模板中的{% url %}(或视图中的 foobar.render())尝试匹配应在 urls.py 中定义的不存在的 url 时,会发生此错误
  • @FoxMaSk 我已经在 urls.py 中添加了相关的 url。但我没有在我的 html 模板中使用{% url %}
  • 你在message=之后做了return吗?
  • 我使用消息发送邮件,发送成功后,我使用return HttpResponseRedirect
  • 您在哪里使用您在urls.py 中定义的名称auth_password_reset_confirm??在您的模板中,您应该删除 A 节点中的所有字符串并将其替换为 {% url auth_password_reset_confirm parm1,parm2,etc %}

标签: python django django-templates


【解决方案1】:

正如 FoxMaSk 所说,您应该替换:

{{ protocol }}://{{ domain }}/accounts/password/reset/confirm/{{uid}}/{{token}}

与:

{{ protocol }}://{{ domain }}{% url 'auth_password_reset_confirm' uid36=uid token=token %}

注意 url 中视图名称周围的单引号,这在 Django 1.5+ 中是必需的(不确定您使用的是什么版本)。

更重要的是我相信错误就在这里:

message = t.render(Context({"domain":"foo.com","protocol":"https"}))

在渲染模板时,您没有将您的 uid 或令牌传递到上下文中,这就是您在这些变量的回溯消息中看到空值的原因:

NoReverseMatch: Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb36': '', u'token': ''}' not found.

【讨论】:

    猜你喜欢
    • 2017-09-05
    • 2020-11-18
    • 1970-01-01
    • 2020-02-10
    • 2016-12-06
    • 2015-09-06
    • 2017-01-07
    • 2020-08-04
    • 1970-01-01
    相关资源
    最近更新 更多