可重用的Django应用程序的模板不呈现

时间:2017-02-16 10:12:01

标签: python django

我有一个单独的应用,我想渲染其模板。

urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^guestbook/', include('guestbook.urls', namespace='guestbook', app_name='guestbook'))
]

留言/ urls.py

from django.conf.urls import url
from . import views

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

留言/ views.py

def index(request):
    entries = Entry.objects.all().order_by('-date')
    return render(request, 'guestbook/index.html', {'entries': entries})

模板/留言/ index.html中

{% extends 'guestbook/base.html' %}
{% block content %}
<a href="{% url 'guestbook:add_comment' entry.pk %}">Comment</a></span>
{% endblock %}

但我收到错误:

NoReverseMatch at /guestbook/
Reverse for 'login' with arguments '()' and keyword arguments '{}' not found.

尝试了0种模式:[]

Error during template rendering
In template /Users/bulrathi/Yandex.Disk.localized/Learning/Code/Test tasks/myproject/templates/guestbook/index.html, error at line 27
Reverse for 'login' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

27 <span class="meta-chunk"><a href="{% url 'guestbook:add_comment' entry.pk %}">Комментировать</a></span>

我会非常感谢你的建议。

1 个答案:

答案 0 :(得分:1)

问题出在base.html模板中。有<a href="{% url 'logout' %}">Выйти</a><a href="{% url 'login' %}">Войти</a>这样的href。将它们更改为<a href="{% url 'guestbook:logout' %}">Выйти</a><a href="{% url 'guestbook:login' %}">Войти</a>可以解决问题。