2016-03-02 17 views
1

我遇到了一個«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+)/$'] 
+0

你的模型中的哪個地方是'context = ...'代碼塊?你能展示你的完整模型嗎? – Gocht

+0

錯誤發生在url http://0.0.0.0:8000/comment/create /'上。您似乎沒有顯示該網址的視圖。 – Alasdair

+0

看起來像「對象」尚未創建。所以它沒有'id' – ilse2005

回答

1

我發現了這個問題。這是打印聲明,我們可以在Traceback中看到:print(new_note)。它是另一種方法的一部分(new_notification)。

我甚至不知道打印可能會導致異常。特別是因爲new_note對象似乎存在(沒有此打印語句,通知運行良好)。

無論如何,非常感謝您的幫助!

相關問題