查看页面时的Python NoReverseMatch

时间:2016-06-17 18:23:53

标签: django login view wiki

我是一个尝试用Django制作维基页面的新手,但是,我一直在遇到这个非常烦人的问题:

NoReverseMatch at /wiki/page/Start/ Reverse for '' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

网站urls.py

    urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^polls/', include('polls.urls')),
    url(r'^wiki/', include('wiki.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

wiki urls.py

app_name = 'wiki'
urlpatterns = [
    url('^', include('django.contrib.auth.urls')),
    url(r'^login/$', auth_views.login, {'template_name': 'wiki/registration/login.html'}, name='login'),
    url(r'^logout/$', auth_views.logout, {'template_name': 'wiki/registration/logged_out.html'}, name='logout'),
    url(r'^register/$',views.register_page, name='register'),
]

urlpatterns += [
    url(r'^page/(?P<page_name>[^/]+)/edit/$',views.edit_page, name='EditPage'),
    url(r'^page/(?P<page_name>[^/]+)/save/$',views.save_page, name='SavePage'),
    url(r'^page/(?P<page_name>[^/]+)/$',views.view_page, name='ViewPage'),
    url(r'^upload/$', views.upload_file, name='upload_page' ),
]

Views.py

def view_page(request, page_name):
    try:
        page = Page.objects.get(pk=page_name)
    except Page.DoesNotExist:
        return render(request,"wiki/create.html", {"page_name":page_name}) 
    content = page.content
    return render(request, "wiki/view.html", {"page_name":page_name, "content":content})

查看用基础插入的模板。此问题适用于我尝试在我的网页上打开的每一件事。我有一个带导航项的导航栏,例如登录或注销,我得到一个非常类似的错误。

{% extends 'wiki/base.html' %}
{% load wiki_extra %}

<html>
        <head>
                <title>{{page_name}}</title>
        </head>
        <body>
            {% block content %}
                <h1>{{page_name}}</h1>
                {% autoescape off %}
                {{content|markup}}
                {% endautoescape %}
                <div class="col-lg-12 text-center">
                <form action="{% url 'wiki:EditPage' page_name=page_name %}">
                    <button class="btn btn-lg btn-danger" type="submit">Edit this page?</button>
                </form>
                </div>
            {% endblock %}
        </body>
</html>

完整错误

Request Method: GET
Request URL:    http://127.0.0.1:8000/wiki/page/Start/
Django Version: 1.9.7
Exception Type: NoReverseMatch
Exception Value:    
Reverse for '' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

编辑:问题出在导航栏部分的base.html中。我发现删除了我在base.html中的部分代码,我基于其他所有文件,发现这些链接在某种程度上破坏了我的代码。谁能告诉我这有什么问题? 我的代码如下所示:

<!-- Navigation -->
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <a href="{% url 'wiki:ViewPage' page_name='Home' %}">
                <img alt="Brand" src="{% static "wiki/img/brand-image.png" %}"">
            </a>
            <a class="navbar-brand" href="{% url 'wiki:ViewPage' page_name=page_name %}">
                <strong>{{page_name}}</strong>
            </a>
        </div>
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav pull-right">
                <li>
                    {% if user.is_authenticated %}
                    <li class="divider-vertical"><a href="{% url 'wiki:upload_page' %}"> <span class = "glyphicon glyphicon-download-alt"> </span> Upload</a></li>
                    <li class="divider-vertical"><a href="{% url 'wiki:logout' %}"> <span class = "glyphicon glyphicon-off"> </span> Log out</a></li>
                    {% else %}
                    <li class="divider-vertical"><a href="{% url login %}"> <span class = "glyphicon glyphicon-off"> </span> Log in</a></li>
                    <li class="divider-vertical"><a href="{% url 'wiki:register' %}"> <span class = "glyphicon glyphicon-pencil"> </span> Sign up</a></li>
                    {% endif %}
                </li>
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
</nav>

1 个答案:

答案 0 :(得分:0)

这是由于您的模板中的这一行:

<li class="divider-vertical"><a href="{% url login %}"> <span class = "glyphicon glyphicon-off"> </span> Log in</a></li>

url模板标记尝试获取login变量的,然后找到反向匹配,但此变量不存在,因此使用空字符串因此没有名字的错误。

  

反向&#39;&#39;参数&#39;()&#39;和关键字参数&#39; {}&#39;没找到。

将其更改为其他所有字符串:

{% url 'wiki:login' %}