前提・実現したいこと
いいね機能実装中に詰まりました
該当のページを開くとNoReverseMatch at /notes/ar1/と表示されてしまいます。
発生している問題・エラーメッセージ
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' not found. 'like' is not a valid view function or pattern name.
該当のソースコード
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 like(request,pk): 12 try: 13 article = Article.objects.get(pk=pk) 14 except Article.DoesNotExist: 15 raise Http404 16 article.like += 1 17 article.save() 18 return redirect(article,pk) 19 20 21def api_like(request,pk): 22 try: 23 article = Article.objects.get(pk=pk) 24 except Article.DoesNotExist: 25 raise Http404 26 article.like += 1 27 article.save() 28 return JsonResponse({"like": article.like})
python
1from django.urls import path 2from . import views 3from notes.views import TemplateView 4 5app_name = 'notes' 6 7urlpatterns = [ 8 path("article/<int:pk>/like/",views.like,name='like'), 9 path("api/like/<int:pk>/",views.api_like,name="api_like"), 10]
python
1{% block content %} 2<body> 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 "like" article.pk %}"><span id="like">{{ article.like }}</span>いいね!</a></p> 10 </div> 11 </a> 12 {% endfor %} 13</body> 14{% endblock %}
「何をした時」にでたエラーなのか書かれていないようです。またTracebackを省略する必要はまったくありません。
https://teratail.com/help/question-tips#questionTips3-4-2
> 自分でタイプしなおしたり、自分で解釈・要約しようとしてはいけません。
コードも明らかにインデントが無くなっていて、読めなくなっています。
回答1件
あなたの回答
tips
プレビュー