簡體   English   中英

模板渲染時發生Django錯誤:找到NoReverseMatch

[英]Django error while template rendering: NoReverseMatch found

以下代碼段引發錯誤。 我搜索了解決方案,但找不到與我的情況有關的任何內容。 任何幫助,將不勝感激。

#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)

以下是針對SO簡化的html文件:

{% 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.

正如FoxMaSk所說,您應該替換:

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

與:

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

請注意網址中視圖名稱的單引號,這在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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM