渲染时捕获NoReverseMatch:反向''带参数'(1,)'和关键字参数'{}'未找到

时间:2011-04-13 02:15:22

标签: django-templates

我的观点:

def display(request):
    feed = SoukFeedMaster.objects.filter(person = request.user)
    return render(request, 'soukfeed/display.html', {'feed' : feed ,})

我的模板:

{% extends "base.html" %}

    {% block content %}    
    {% for x in feed %}
        {% load url from future %}
        <a href="{% url x.content.url_internal_django_link  x.content.id  %}">  {{x.content.content}} </a>
        <br/> 
    {% endfor %}

    {% endblock %}

追溯:

Environment:


Request Method: GET
Request URL: http://localhost:8000/soukfeed/

Django Version: 1.3
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.comments',
 'ec.kiosk',
 'ec.chakra',
 'ec.ajax',
 'ec.broadcast',
 'ec.connect',
 'ec.seek_solutions',
 'ec.feed',
 'ec.ec_model',
 'ec.info',
 'ec.souk_info',
 'ec.ec_central',
 'ec.domains',
 'ec.souk',
 'ec.souk_feed',
 'ec.meta',
 'django.contrib.admin']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Template error:
In template /volumes/disk2/workspace/templates/ec/soukfeed/display.html, error at line 1
   Caught NoReverseMatch while rendering: Reverse for '' with arguments '(1,)' and keyword arguments '{}' not found.
   1 : {% extends "base.html" %}

    {% block content %}    
     {% for x in feed %} 
        {% load url from future %}
        <a href="{% url x.content.url_internal_django_link  x.content.id  %}">  {{ x.content.content }} </a>
        <br/> 
    {% endfor %}

    {% endblock %}


Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Volumes/Disk2/workspace/ec/ec/souk_feed/views.py" in display
  18.         return render(request, 'soukfeed/display.html', {'feed' : feed ,})
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render
  44.     return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  188.         return t.render(context_instance)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py" in render
  123.             return self._render(context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  127.         return compiled_parent._render(context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  64.             result = block.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  227.                 nodelist.append(node.render(context))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  450.                         raise e

Exception Type: TemplateSyntaxError at /soukfeed/
Exception Value: Caught NoReverseMatch while rendering: Reverse for '' with arguments '(1,)' and keyword arguments '{}' not found.

5 个答案:

答案 0 :(得分:7)

我的应用程序遇到了同样的问题。以下为我解决了这个问题:

因为您使用{% url %}标记生成视图的链接,所以需要将应用程序的URL添加到项目的根URLConf模块(通过include()调用)。如果您将{% url %}标记与您尚未在项目中设置的URL名称一起使用,则无法找到正确的URL,只会返回空字符串而不是URL。 / p>

所以基本上问题是你正在查找的url必须在项目的urls.py中,否则Django无法找到并跟踪它。

答案 1 :(得分:1)

信息真的不够。但问题出在这里:

{% url x.content.url_internal_django_link  x.content.id  %}

可能你需要那个:

{% extends "base.html" %}

{% block content %}    
{% for x in feed %}
    <a href="{{ x.content.get_absolute_url }}">  {{x.content.content}} </a>
    <br/> 
{% endfor %}

{% endblock %}

但我不确定 - 这取决于你的模型代码和你的一般逻辑

答案 2 :(得分:0)

  

异常值:捕获NoReverseMatch   渲染时:反转''with   参数'(1,)'和关键字参数   找不到“{}”。

看起来你的x.content.url_internal_django_link正在返回一个空字符串。

检查您的数据库或暂时将{% url %}标记更改为:

{{ x.content.url_internal_django_link }}

查看存储内容以及从db获取空字符串的原因。

答案 3 :(得分:0)

我有相同的异常输出,说不能'(93L,)'并且找不到关键字参数。

检查urls.py,在那里你可以看到那里没有提供网址。所以提供网址然后你不会出现这个错误。

答案 4 :(得分:0)

另一种可能性是URL的正则表达式字符串与您的request.user.username

不匹配