1

I get stuck on a « NoReverseMatch » exception with a reverse.

Reverse for 'notifications_read' with arguments '()' and keyword arguments '{'id': None}' not found. 1 pattern(s) tried: ['notifications/read/(?P\d+)/$']

I’m trying to check when notifications are read on comments. Basically, I’ve got this so far:

url.py:

url(r'^notifications/read/(?P<id>\d+)/$', notifications.views.read, name='notifications_read')

And the problem seems to occur in the class Notification, in the str(self) method of:

notifications/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)

I can’t figure out why self.id is None which seems to be the problem, since when I take it off in the URL pattern, as well as the kwargs in the reverse, I don’t get the exception (but neither the needed URL).

If I put str(self.id) as:

reverse('notifications_read', kwargs={'id': str(self.id)})

with

url(r'^notifications/read/(?P<id>[\w-]+)/$', notifications.views.read, name='notifications_read')

there is no exception but the rendering is /notifications/read/None/

I know there are lots of similar questions out there like Reverse for '*' with arguments '()' and keyword arguments '{}' not found

But in none of them, there seem to be this problem with self.id returning None.

I’m using Django 1.9.2 with python 3.5

Could you help? It would be really appreciated. Thanks a lot!

If it can be helpful, the exception is also not occurring when I comment those lines:

In comments/views.py

notify.send(
    request.user,
    action=new_comment,
    target=parent_comment,
    recipient=parent_comment.user,
    verb="replied to"
)

And here is the corresponding view in notifications/views.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"))

On the debug page, I get:

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 (full Traceback):

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+)/$']
Community
  • 1
  • 1
  • Where in your models is the `context = ...` code block? Can you show your full model? – Gocht Mar 02 '16 at 16:15
  • The error is occuring on the url `http://0.0.0.0:8000/comment/create/`. You don't appear to have shown the view for that url. – Alasdair Mar 02 '16 at 16:28
  • Looks like the `object` has not been created yet. So it has no `id` – ilse2005 Mar 02 '16 at 16:30
  • Thanks for your answers. I edited the question (after EDIT at the bottom of the question) with the full model and the view corresponding to /comment/create/. I wanted to put as little as possible for you not to have to much info, but it seems it wasn't enough (I'm kind of a newbie) – Léo Chaz Maltrait Mar 02 '16 at 17:44
  • Is that the full traceback from the debug page? – Alasdair Mar 02 '16 at 17:51
  • It wasn't, I'm really sorry. I just put it after the EDIT 2 at the bottom of the question. – Léo Chaz Maltrait Mar 02 '16 at 18:56

1 Answers1

1

I found out the problem. It was the print statement that we can see in the Traceback: print(new_note). It was part of another method (new_notification).

I didn't even know a print could cause exceptions. Especially since the new_note object seems to exist (the notification are working well without this print statement).

Anyway, thanks a lot for your time and help guys!