Django NoReverseMatch错误,尝试了0种模式

时间:2015-12-06 02:29:44

标签: python django

我已尝试过针对此问题提出的大约一千种不同的修复方法,但似乎没有任何效果。

urls.py(app):

from django.contrib.auth.decorators import login_required
from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', login_required(views.index), name='index'),
    url(r'^clock/', views.clock, name='clock'),
]

urls.py(项目):

from django.conf import settings
from django.contrib.auth import views as auth_views
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.static import static
from mqtt.views import auth, acl, superuser


urlpatterns = [
    url('^accounts/login/', auth_views.login,
        {'template_name': 'login.html'}
    ),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^auth$', auth),
    url(r'^superuser$', superuser),
    url(r'^acl$', acl),
    url(r'^$', include('lamp.urls')),
]
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

views.py:

from django.shortcuts import render

def index(request):
    lamps = request.user.lamp_set.all()
    context = {}

    if lamps:
        device_id = lamps[0].model
        context = {'device_id': device_id}

    return render(request, 'index.html', context)

def clock(request):
    return render(request, 'clock.html')

index.html(摘录):

<div class="toggle-button" id="power"></div>
<div class="toggle-button" id="alarm-clock">
<a href="{% url 'clock' %}" id='clock-link'>Clock</a>
</div>

当我尝试加载页面时,我得到了:

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

我对Django很新,所以任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

$表示urlconf搜索django的结束。您需要将其删除以启用内部应用程序网址搜索。

urlpatterns = [
    url(r'^', include('lamp.urls')), # <----
    #... rest of the urlconfs       
]