Django初心者です。ブログアプリのようなものを作っています。
フォームに入力した内容の詳細を表示させるページに行こうとするとこのようなエラーが出ます。フォームに入力した内容を変更するページ(http://127.0.0.1:8000/KnowHow/1/update/)などは開けるのですが,なぜか詳細を表示するリンク(http://127.0.0.1:8000/KnowHow/1/detail/)にアクセスしようとするとエラーが表示されてしまいます。
NoReverseMatch at /KnowHow/1/detail/ Reverse for 'update' with arguments '('',)' not found. 1 pattern(s) tried: ['KnowHow\/(?P<pk>[0-9]+)\/update\/$']
views.py,detailページのhtml,urls.py,models.pyを置きました。推測でもいいのでコメントを頂けたら幸いです。他にも必要なコードがあればコメントください。
views.py
from django.urls import reverse, reverse_lazy from django.http import HttpResponse from django.views import generic from django.shortcuts import redirect from django.core.exceptions import PermissionDenied from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.decorators import login_required from .models import KnowHow class IndexView(generic.list.ListView): model = KnowHow paginate_by = 5 ordering = ['-title'] # template_name = 'portal/KnowHow_list.html' #該当の関数 class DetailView(generic.detail.DetailView): model = KnowHow #template_name = 'portal/KnowHow_detail.html' class CreateView(LoginRequiredMixin, generic.edit.CreateView): # The LoginRequired mixin model = KnowHow fields = ['title','text','basic_tag','free_tags'] #template_name = 'portal/KnowHow_form.html' def form_valid(self, form): form.instance.author = self.request.user return super(CreateView, self).form_valid(form) class UpdateView(LoginRequiredMixin, generic.edit.UpdateView): # The LoginRequired mixin model = KnowHow fields = ['title','text','basic_tag','free_tags'] #template_name = 'portal/KnowHow_form.html' def dispatch(self, request, *args, **kwargs): # ownership validation obj = self.get_object() if obj.author != self.request.user: raise PermissionDenied('You do not have permission to edit.') return super(UpdateView, self).dispatch(request, *args, **kwargs) class DeleteView(LoginRequiredMixin, generic.edit.DeleteView): # The LoginRequired mixin model = KnowHow success_url = reverse_lazy('portal:index') #template_name = 'portal/KnowHow_confirm_delete.html' def dispatch(self, request, *args, **kwargs): # ownership validation obj = self.get_object() if obj.author != self.request.user: raise PermissionDenied('You do not have permission to delete.') return super(DeleteView, self).dispatch(request, *args, **kwargs) @login_required def help(request): return HttpResponse("Member Only Help Page")
portal/KnowHow_detail.html
html
1 2{% extends 'base.html' %} 3 4{% block title %}Sign up{% endblock %} 5 6{% block content %} 7<h1>{{ object.title }}</h1> 8<section class="KnowHow-text"> 9 {{ object.text|linebreaksbr }} 10 <p>Author: {{ object.author }}</p> 11 <p>Free tags: {{ object.free_tags}}</p> 12 <p>Basic tag: {{ object.basic_tag}}</p> 13</section> 14 15 16{# ownership #} 17{% if request.user.id == object.author_id %} 18<section class="KnowHow-edit"> 19 <a class="act" href="{% url 'portal:update' KnowHow.pk %}">Edit</a> 20 <a class="act" href="{% url 'portal:delete' KnowHow.pk %}">Delete</a> 21</section> 22{% endif %} 23<section> 24 <p><a href="javascript:history.back()">< Back</a></p> 25</section> 26{% endblock %}
urls.py
from django.urls import path from . import views # set the application namespace # https://docs.djangoproject.com/en/2.0/intro/tutorial03/ app_name = 'portal' urlpatterns = [ # ex: / path('', views.IndexView.as_view(), name='index'), # ex: /KnowHow/create/ path('KnowHow/create/', views.CreateView.as_view(), name='create'), # ex: /KnowHow/1/ path('KnowHow/<int:pk>/detail/', views.DetailView.as_view(), name='detail'), # ex: /KnowHow/1/update/ path('KnowHow/<int:pk>/update/', views.UpdateView.as_view(), name='update'), # ex: /KnowHow/1/delete path('KnowHow/<int:pk>/delete/', views.DeleteView.as_view(), name='delete'), # ex: /KnowHow/help/ path('KnowHow/help/', views.help, name='help'), ]
models.py
from django.db import models from django.urls import reverse from taggit.managers import TaggableManager class KnowHow(models.Model): BASIC_TAGS =( ('1','a'), ('2','b'), ('3','c'), ) author = models.ForeignKey('auth.User',on_delete=models.CASCADE) title = models.CharField(max_length=200) text = models.TextField() basic_tag = models.CharField(max_length=1,choices=BASIC_TAGS,default='1') free_tags = TaggableManager(blank=True) def __str__(self): return self.title def get_absolute_url(self): return reverse('portal:index')
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/10/11 04:25
2019/10/11 04:29