前提・実現したいこと
Django初心者です。
Djangoでウェブページを作っていて、データベースから取得したデータを簡易的に表示して、詳細のリンクをクリックすると、その1行の詳細情報を表示するプログラムを作りたいのですが、うまくいきません。
テンプレート1でクリックされた値をviewへ渡す→SQL文を実行→結果をテンプレート2へ渡して表示を想定していますが、最初のクリックされた値をviewへ渡すところから躓いています。
やりたいこと
試したこと(色々試した後に書いたのでごちゃごちゃしてる)
DjangoチュートリアルとDjangoで、汎用ビュー(ListView, DetailView)を参考に
#urls.py app_name = 'Number' urlpatterns = [ url(r'^$', Number.views.index, name='index'), url(r'^detail/(?P<pk>[0-9]+)/$', Number.views.post_detail, name='post_detail'),]
<!--Number.html(一部)--> <a href="{% url 'Number:detail' pk=post.pk %}">{{ post.0 }}</a>
app_name='Number'を書いたのに、エラーが出ました。(最初に書かれていなかったので追加した)
NoReverseMatch at /Number/ 'Number' is not a registered namespace
または、Django Girls のチュートリアルを参考に
#views.py def post_detail(request, pk): post = get_object_or_404(Post, pk=pk) return render(request, 'Number/post_detail.html', {'post': post})
#urls.py path('detail/<int:pk>/', Number.views.post_detail, name='post_detail'),
<!--Number.html--> <a href="{% url 'post_detail' pk=post.pk %}">{{ post.0 }}</a>
とやるとエラー
NoReverseMatch at /Number/ Reverse for 'post_detail' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['detail/(?P<pk>[0-9]+)/$']
urls.pyの設定でのエラーも参考に、
<!--index.html--> <a href="(% url 'post_detail' pk=post.pk %)">{{ post.0 }}</a>
{}を()にしたら簡易の一覧表示までは行ったんですが、詳細のurlが
#詳細にカーソルを合わせるとこの表示 http://localhost:*****/Number/(% url 'post_detail' pk=post.pk %) #クリックした先のリンク http://localhost:*****/Nunber/(%25%20url%20'post_detail'%20pk%3Dpost.pk%20%25)
となり、クリックすると、Page not found (404)と出てしまいます
理想のリンク先
http://localhost:*****/detail/ABC01