Reputation: 962
I am currently working on a website and I am receiving an error that is driving me up a wall. I have the following url defined in urls.py
.
urlpatterns = [
url(r'^', views.homeFeed, name='home'),
url(r'^posts/(?P<uid>[\w+])$', views.discussPostView, name='post')
]
I then have the following code defining views.discussPostView
.
def discussPostView(request, **kwargs):
if request.method == 'GET':
print(kwargs['uid'])
post = get_object_or_404(DiscussPost, uid=kwargs['uid'])
return render(request, 'discuss/post.html', {"post": post})
if request.method == 'POST':
reply = DiscussReply.objects.create(
by=request.user,
content=request.POST['reply'])
reply.save()
reply_to = DiscussPost.objects.get(pk=request.POST['post_id'])
reply_to.replies.add(reply)
reply_to.save()
return HttpResponseRedirect(request.get_full_path())
However, when I try to create a post on the site or access a post with a certain uid, I receive this error;
Reverse for 'post' with arguments '()'
and keyword arguments '{u'uid': u'CHacFvE_'}' not found. 1 pattern(s) tried: [u'discuss/posts/(?P<uid>[\\w+])$']
I literally have no idea what's wrong with my code. I am a bit rusty on python so I might be missing some obvious pointers, but any pointers in the right direction would be helpful. Thank you!
Full error stack:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ubuntu/workspace/discuss/views.py", line 15, in homeFeed
"feeds": [DiscussPost.getFeed(request), DiscussPost.getNewPosts()]})
File "/usr/local/lib/python2.7/dist-packages/django/shortcuts.py", line 67, in render
template_name, context, request=request, using=using)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 97, in render_to_string
return template.render(context, request)
File "/usr/local/lib/python2.7/dist-packages/django/template/backends/django.py", line 95, in render
return self.template.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 206, in render
return self._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 197, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 988, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 955, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 173, in render
return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 197, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 988, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 955, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 69, in render
result = block.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 988, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 955, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 326, in render
return nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 988, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 955, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 220, in render
nodelist.append(node.render_annotated(context))
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 955, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 220, in render
nodelist.append(node.render_annotated(context))
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 955, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 513, in render
six.reraise(*exc_info)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 499, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 600, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 508, in _reverse_with_prefix
(lookup_view_s, args, kwargs, len(patterns), patterns))
NoReverseMatch: Reverse for 'post' with arguments '()' and keyword arguments '{u'uid': u'-lTmOw__'}' not found. 1 pattern(s) tried: [u'discuss/posts/(?P<uid>[\\w]+)$']
Upvotes: 0
Views: 66
Reputation: 23064
What is the valid characters in the Post.uid? Your question has two examples u'CHacFvE_'
from the error message and {u'uid': u'-lTmOw__'}
in the stack trace.
If you want both underscores and dashes to be valid in uid, you must change the regex.
^posts/(?P<uid>[-\w]+)$
explanation: https://regex101.com/r/1E2WVY/1
Upvotes: 1