前提・実現したいこと
いいね機能を実装中に以下のエラーメッセージが発生しました。
開発用サーバーを起動し、テンプレートのページを開いた時にエラーが発生し、テンプレートの<p><a href="{% url 'notes:like' article.pk %}"><span id="like">{{ article.like }}</span>いいね!</a></p>のところでエラーになっていました。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/yoshihiro/Desktop/HBGB/mysite/notes/views.py", line 37, in note_ar1 return render(request,'notes/note_ar1.html',{'notes': notes}) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/shortcuts.py", line 19, in render content = loader.render_to_string(template_name, context, request, using=using) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/template/loader.py", line 62, in render_to_string return template.render(context, request) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render return self.template.render(context) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/template/base.py", line 170, in render return self._render(context) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/template/base.py", line 162, in _render return self.nodelist.render(context) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/template/base.py", line 938, in render bit = node.render_annotated(context) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated return self.render(context) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/template/loader_tags.py", line 53, in render result = self.nodelist.render(context) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/template/base.py", line 938, in render bit = node.render_annotated(context) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated return self.render(context) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/template/defaulttags.py", line 211, in render nodelist.append(node.render_annotated(context)) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated return self.render(context) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/template/defaulttags.py", line 446, in render url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/urls/base.py", line 87, in reverse return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)) File "/Users/yoshihiro/.local/share/virtualenvs/mysite-xs0jetOT/lib/python3.8/site-packages/django/urls/resolvers.py", line 685, in _reverse_with_prefix raise NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch: Reverse for 'like' with arguments '('',)' not found. 1 pattern(s) tried: ['notes/article/(?P<pk>[0-9]+)/like/$']
該当のソースコード
python
1from django.urls import path 2from . import views 3 4 5app_name = 'notes' 6 7urlpatterns = [ 8 path('ar1/',views.note_ar1,name='note_ar1'), 9 path("article/<int:pk>/like/",views.like,name='like'), 10 path("api/like/<int:pk>/",views.api_like,name="api_like"),
python
1from django.shortcuts import render, redirect 2from django.views import generic 3from .models import Note,Article 4from django.urls import reverse_lazy 5from django.contrib.auth.mixins import LoginRequiredMixin 6from django.views.generic import TemplateView 7from django.http import Http404 8from django.http.response import JsonResponse 9from .models import Article 10 11def note_ar1(request): 12 notes = Note.objects.filter(grade="1",sub="11") 13 return render(request,'notes/note_ar1.html',{'notes': notes}) 14 15def like(request,pk): 16 try: 17 article = Article.objects.get(pk=pk) 18 except Article.DoesNotExist: 19 raise Http404 20 article.like += 1 21 article.save() 22 return redirect(article,pk) 23 24 25def api_like(request,pk): 26 try: 27 article = Article.objects.get(pk=pk) 28 except Article.DoesNotExist: 29 raise Http404 30 article.like += 1 31 article.save() 32 return JsonResponse({"like": article.like})
python
1{% block content %} 2<body bgcolor="#00ffff"> 3 {% for note in notes %} 4 <a href=""> 5 <div class="note"> 6 <a href="{{ note.file.url }}" target="_blank"><img src="{{ note.file.url }}" width="300" alt="" border="0"></a> 7 <h3>{{ note.title }}</h3> 8 <h5>投稿日{{ note.date }}</h5> 9 <p><a href="{% url 'notes:like' article.pk %}"><span id="like">{{ article.like }}</span>いいね!</a></p> 10 </div> 11 </a> 12 {% endfor %} 13</body> 14{% endblock %}
試したこと
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー