質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1027閲覧

NoReverseMatchについて

yoshihiro27

総合スコア1

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/01/05 09:24

編集2021/01/05 10:39

前提・実現したいこと

いいね機能実装中に詰まりました
該当のページを開くと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 %}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

quickquip

2021/01/05 09:33

「何をした時」にでたエラーなのか書かれていないようです。またTracebackを省略する必要はまったくありません。 https://teratail.com/help/question-tips#questionTips3-4-2 > 自分でタイプしなおしたり、自分で解釈・要約しようとしてはいけません。 コードも明らかにインデントが無くなっていて、読めなくなっています。
guest

回答1

0

ベストアンサー

urls.pyでapp_nameを指定しているので、

HTML

1<a href="{% url "like" article.pk %}"> 2```を、 3```HTML 4<a href="{% url 'notes:like' article.pk %}"> 5```にします。 6あと、文字列の中の文字列は、シングルクォーテーションとダブルクォーテーションで分けます。

投稿2021/01/05 21:43

編集2021/01/05 21:45
ForestSeo

総合スコア2720

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問