エラーが起きて対処できません。
https://stackoverflow.com/questions/61406939/reverse-for-post-edit-with-arguments-not-found-1-patterns-triedサイトを参考にしようとしたのですが解決できませんでした。
解決方法を教えて下さい。お願い致します。
html
1{% load static %} 2 3<!DOCTYPE html> 4<html lang="ja"> 5 <head> 6 <meta charset="UTF-8" /> 7 <meta name="viewport" content="width=device-width, initial-scale=1.0" /> 8 <link 9 rel="stylesheet" 10 href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" 11 /> 12 <link rel="stylesheet" href="{% static 'css/style.css' %}" /> 13 <title>ブログチュートリアル</title> 14 </head> 15 16 <body> 17 <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> 18 <div class="container"> 19 <a class="navbar-brand" href="/">ブログ</a> 20 <ul class="navbar-nav ml-auto"> 21 <li class="nav-item"> 22 <a class="nav-link" href="/">ホーム</a> 23 </li> 24 {% if user.is_authenticated %} 25 <li class="nav-item"> 26 <a class="nav-link" href="{% url 'post_new' %}">投稿</a> 27 </li> 28 <li class="nav-item"> 29 <a class="nav-link" href="">ログアウト</a> 30 </li> 31 {% else %} 32 <li class="nav-item"> 33 <a class="nav-link" href="">サインアップ</a> 34 </li> 35 <li class="nav-item"> 36 <a class="nav-link" href="">ログイン</a> 37 </li> 38 {% endif %} 39 </ul> 40 </div> 41 </nav>
pyhton
1from django.urls import path 2from app import views 3 4urlpatterns = [ 5 path('', views.IndexView.as_view(), name='index'), 6 path('post/<int:pk>/', views.PostDetailView.as_view(), name='post_detail'), 7 path('post/nwe/', views.CreatePostlView.as_view(), name='post_new'), 8 path('post/<int:pk>/edit/', views.PostEditView.as_view(), name='post_edit'), 9] 10
python
1from django.http.request import RawPostDataException 2from django.shortcuts import render, redirect 3from django.views.generic import View 4from .models import Post 5from .forms import PostForm 6from django.contrib.auth.mixins import LoginRequiredMixin 7class IndexView(View): 8 def get(self, request , *args, **kwargs): 9 post_data=Post.objects.order_by('-id') 10 return render(request , 'app/index.html',{ 11 'post_data': post_data 12 }) 13 14class PostDetailView(View): 15 def get(self, request, *args, **kwargs): 16 post_data=Post.objects.get(id = self.kwargs['pk']) 17 return render(request, 'app/post_detail.html', { 18 'post_data':post_data 19 }) 20 21class CreatePostlView(LoginRequiredMixin,View): 22 def get(self, request, *args, **kwargs): 23 form=PostForm(request.POST or None) 24 return render(request, 'app/post_form.html',{ 25 'form':form 26 }) 27 28 def post(self, request, *args, **kwargs): 29 form=PostForm(request.POST or None) 30 31 if form.is_valid(): 32 post_data=Post() 33 post_data.author=request.user 34 post_data.title=form.cleaned_data['title'] 35 post_data.content=form.cleaned_data['content'] 36 post_data.save() 37 return redirect('post_detail' , post_data.id) 38 39 return render(request, 'app/post_form.html',{ 40 'form': form 41 }) 42 43class PostEditView(LoginRequiredMixin,View): 44 def get(self, request, *args, **kwargs): 45 post_data=Post.objects.get(id=self.kwargs['pk']) 46 form=PostForm( 47 request.POST or None, 48 initial={ 49 'title': post_data.title, 50 'content': post_data.content 51 } 52 ) 53 54 return render(request, 'app/post_form.html',{ 55 'form':form 56 }) 57 58 def post(self, request, *args, **kwargs): 59 form=PostForm(request.POST or None) 60 61 if form.is_valid(): 62 post_data=Post.objects.get(id=self.kwargs['pk']) 63 post_data.title=form.cleaned_data['title'] 64 post_data.content=form.cleaned_data['content'] 65 post_data.save() 66 return redirect('post_detail' , self.kwargs['pk']) 67 68 return render(request, 'app/post_form.html',{ 69 'form': form 70 }) 71
あなたの回答
tips
プレビュー