Django中的NoReverseMatch;视图和网址已正确定义

时间:2017-03-29 19:55:27

标签: python django

我目前正在网站上工作,我收到的错误正在推动我的问题。我在urls.py中定义了以下网址。

urlpatterns = [
    url(r'^', views.homeFeed, name='home'),
    url(r'^posts/(?P<uid>[\w+])$', views.discussPostView, name='post')
    ]

然后我有以下代码定义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())

但是,当我尝试在网站上创建帖子或使用某个uid访问帖子时,我收到此错误;

Reverse for 'post' with arguments '()' 
and keyword arguments '{u'uid': u'CHacFvE_'}' not found. 1 pattern(s) tried: [u'discuss/posts/(?P<uid>[\\w+])$']

我完全不知道我的代码有什么问题。我在python上有点生疏,所以我可能会遗漏一些明显的指针,但是正确方向的任何指针都会有所帮助。谢谢!

完整错误堆栈:

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]+)$']

2 个答案:

答案 0 :(得分:1)

Post.uid中的有效字符是什么?您的问题在错误消息中有两个示例u'CHacFvE_',在堆栈跟踪中有{u'uid': u'-lTmOw__'}

如果您希望下划线和短划线在uid中有效,则必须更改正则表达式。

^posts/(?P<uid>[-\w]+)$

解释:https://regex101.com/r/1E2WVY/1

答案 1 :(得分:0)

你的模式错了,试试这个:

'^posts/(?P<uid>\w+)$'