私は次のようにURLを設定して渡そうとしています:
/details/12345
テンプレート HTML:
<div class="row">
{% if article_list %}
{% for article in article_list %}
<div>
<h2>{{ article.title }}</h2>
<p>{{ article.body }}</p>
<p><a class="btn btn-default" href="{% url 'details' article.id %}" role="button">View details »</a></p>
</div><!--/.col-xs-6.col-lg-4-->
{% endfor %}
{% endif %}
</div><!--/row-->
urls.py (フル):
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'news_readr.views.home', name='home'),
url(r'^details/(?P<article_id>\d+)/$', 'news_readr.views.details', name='details'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
ビュー.py:
from django.shortcuts import render
from .models import Article
# Create your views here.
def home(request):
title = "Home"
article_list = Article.objects.all()
for article in article_list:
print(article.id)
context = {
"title": title,
"article_list": article_list,
}
return render(request, "home.html", context)
def details(request, article_id = "1"):
article = Article.objects.get(id=article_id)
return render(request, "details.html", {'article': article})
次のようなエラーが表示されます。
NoReverseMatch at /
Reverse for 'details' with arguments '()' and keyword arguments '{}'
not found. 1 pattern(s) tried: ['details/(?P<article_id>\\d+)/$']
私は Django を使用して 1 週間経ちましたが、URL 名前付きグループの構成に何か問題があると思います。助けてください!ティア!
更新: URL 構成を削除して元に戻すと:
url(r'^details/$', 'news_readr.views.details', name='details'),
エラーは次のように変わります。
引数 '(1,)' とキーワード引数 '{}' を持つ 'details' の逆が見つかりません。1 つのパターンが試行されました: ['details/$']
1
したがって、この場合に渡される引数を取得しているようです。したがって、これは正規表現の問題のようです。Pythex で式を試してみましたが、それでも式が何にも一致しないようです。