我遇到了一个«NoReverseMatch»异常,反过来。'<a_view>'的反向参数为'()'和关键字参数'{'id':None}'not found
对于带有参数'()'和关键字参数'{'id':None}'的'notifications_read'没有找到。 1模式尝试:['通知/阅读/(?P \ D +)/ $']
我试图检查何时通知读取评论。基本上,我有这个至今:
url.py:
url(r'^notifications/read/(?P<id>\d+)/$', notifications.views.read, name='notifications_read')
而且问题似乎在班上通知出现在的STR(个体经营)方法:
通知/ models.py
context = {
"sender": self.sender_object,
"verb": self.verb,
"target": self.target_object,
"action": self.action_object,
"verified_read": reverse('notifications_read', kwargs={"id": self.id}),
"target_url": target_url,
}
return "{sender} {verb} <a href='{verified_read}?next={target_url}'>{target}</a> with {action}.".format(**context)
我想不通为什么self.id是None,这似乎是问题,因为当我在URL模式中以及反过来使用kwargs时,我没有得到异常(但不是所需的URL)。
如果我把STR(self.id)为:
reverse('notifications_read', kwargs={'id': str(self.id)})
与
url(r'^notifications/read/(?P<id>[\w-]+)/$', notifications.views.read, name='notifications_read')
有也不例外,但渲染/通知/读/无/
我知道有很多类似的问题,如Reverse for '*' with arguments '()' and keyword arguments '{}' not found
但是在其中没有一个,self.id返回None似乎存在这个问题。
我使用Django的1.9.2和Python 3.5
你能帮忙吗?这将非常感激。 非常感谢!
如果它可以帮助,异常也没有,当我评论这些线路发生:
在的意见/ views.py
notify.send(
request.user,
action=new_comment,
target=parent_comment,
recipient=parent_comment.user,
verb="replied to"
)
这里是在相应的视图通知/视图。PY:
@login_required
def read(request, id):
try:
next = request.GET.get('next', None)
notifications = Notification.objects.get(id=id)
if notifications.recipient == request.user:
notifications.read = True
notifications.save()
if next is not None:
return HttpResponseRedirect(next)
else:
return HttpResponseRedirect(reverse("notifications_all"))
else:
raise Http404
except:
raise HttpResponseRedirect(reverse("notifications_all"))
在的调试页面,我得到:
Request Method: POST
Request URL: http://0.0.0.0:8000/comment/create/
Django Version: 1.9.2
Exception Type: NoReverseMatch
Exception Value: Reverse for 'notifications_read' with arguments '()' and keyword arguments '{'id': None}' not found. 1 pattern(s) tried: ['notifications/read/(?P<id>\\d+)/$']
Exception Location: /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 508
Python Executable:/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
Python Version:3.5.0
EDIT 2(全回溯):
Environment:
Request Method: POST
Request URL: http://0.0.0.0:8000/comment/create/
Django Version: 1.9.2
Python Version: 3.5.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'accounts',
'comments',
'notifications',
'videos']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/leomaltrait/PycharmProjects/srvup/src/comments/views.py" in comment_create_view
80. verb="commented on"
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/dispatch/dispatcher.py" in send
192. response = receiver(signal=self, sender=sender, **named)
File "/Users/leomaltrait/PycharmProjects/srvup/src/notifications/models.py" in new_notification
157. print(new_note)
File "/Users/leomaltrait/PycharmProjects/srvup/src/notifications/models.py" in __str__
95. "verified_read": reverse('notifications_read', kwargs={"id": self.id}),
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/urlresolvers.py" in reverse
600. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
508. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /comment/create/
Exception Value: Reverse for 'notifications_read' with arguments '()' and keyword arguments '{'id': None}' not found. 1 pattern(s) tried: ['notifications/read/(?P<id>\\d+)/$']
你的模型中的哪个地方是'context = ...'代码块?你能展示你的完整模型吗? – Gocht
错误发生在url http://0.0.0.0:8000/comment/create /'上。您似乎没有显示该网址的视图。 – Alasdair
看起来像“对象”尚未创建。所以它没有'id' – ilse2005