我正在尝试为同学们制作一个小的django“board”应用程序。 但我收到这个错误,我无法找到解决办法。 我认为它必须与模板渲染有关,但我不知道问题是什么。它工作正常,直到我对视图进行了一些修改。下面的模板。即使在我解除了我所做的所有更改之后,它仍然停止工作并返回相同的错误。
错误
NoReverseMatch at /board/2/
Reverse for 'read_article' with arguments '('', 10)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['board/(?P<board_id>\\d+)/(?P<article_id>\\d+)/$']
Request Method: GET
Request URL: http://192.168.56.101:8000/board/2/
Django Version: 1.7.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'read_article' with arguments '('', 10)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['board/(?P<board_id>\\d+)/(?P<article_id>\\d+)/$']
Exception Location: /home/web/venv/lib/python3.4/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 468
Python Executable: /home/web/venv/bin/python
Python Version: 3.4.2
Views.py
def index(request):
boards = Board.objects.order_by("title")
dashboards = []
try:
if request.session["error"]:
error_message = request.session["error"]
del request.session["error"]
except KeyError:
error_message = None
for board in boards:
articles = board.article_set.order_by("-written_date")[:5]
dashboard_item = {
"board" : board,
"articles" : articles,
}
dashboards.append(dashboard_item)
context = {
"boards" : Board.objects.all(),
"dashboards" : dashboards,
"error_message" : error_message,
}
return render(request, "index.html", context)
def read_board(request, board_id):
board = get_object_or_404(Board, id=board_id)
article_list = board.article_set.order_by("-written_date")
paginator = Paginator(article_list, 5)
page = request.GET.get("page")
try:
articles = paginator.page(page)
except PageNotAnInteger:
articles = paginator.page(1)
except EmptyPage:
articles = paginator.page(paginator.num_pages)
context = {
"articles" : articles,
"pages" : paginator.page_range
}
return render(request, "board.html", context)
def read_article(request, board_id, article_id):
article = get_object_or_404(Article, id=article_id)
reply_list = article.reply_set.order_by("written_date")
try:
if request.session["error"]:
error_message = request.session["error"]
del request.session["error"]
except KeyError:
error_message = None
context = {
"boards" : Board.objects.all(),
"board_id" : board_id,
"article" : article,
"replies" : reply_list,
"error_message" : error_message,
}
return render(request, "article.html", context)
Urls.py
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'board.views.index', name = 'index'),
url(r'^board/(?P<board_id>\d+)/$', 'board.views.read_board', name = 'board'),
url(r'^board/(?P<board_id>\d+)/(?P<article_id>\d+)/$', 'board.views.read_article', name = 'read_article'),
url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name='login'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
模板
{% include "header.html" %}
{% include "navbar.html" %}
<div class="container">
<div class="page-header">
<h3>{{ board.title }}</h3>
</div>
<div class="list-group">
<div class="list-group-item list-group-item-info text-center">
<div class="row text-center">
<div class="col-xs-2 col-md-2">글번호</div>
<div class="col-xs-4 col-md-5">글제목</div>
<div class="col-xs-3 col-md-2">작성자</div>
<div class="col-xs-3 col-md-3">작성시간</div>
</div>
</div>
{% if articles %}
{% for article in articles %}
<a href="{% url 'read_article' board.id article.id %}" class="list-group-item">
<div class="row text-center">
<div class="col-xs-2 col-md-2">{{ article.id }}</div>
<div class="col-xs-4 col-md-5">{{ article.title }}</div>
<div class="col-xs-3 col-md-2">{{ article.user.first_name }} {{ article.user.last_name }}</div>
<div class="col-xs-3 col-md-3">{{ article.written_date }}</div>
</div>
</a>
{% endfor %}
{% else %}
<div class="list-group-item text-center">작성된 글이 없습니다</div>
{% endif %}
</div>
<a href="{% url 'new_article' board.id %}" class="btn btn-success pull-right">새글쓰기</a>
{% include "paginator.html" %}
</div>
{% include "footer.html" %}
答案 0 :(得分:1)
从追溯中我发现你在模板中的url标签中提供的board.id是空的,事实上,你还没有在你的上下文中指定它。