前提・実現したいこと
https://django.kurodigi.com/
こちらのサイトを参考にdjangoで掲示板サイトを作成したところ、トップページから投稿をするボタンをクリックしたらエラーメッセージが発生してしまいました。
発生している問題・エラーメッセージ
NoReverseMatch at /thread/create_topic/
Reverse for 'create_topic.html' not found. 'create_topic.html' is not a valid view function or pattern name.
該当のソースコード
urls
1from django.urls import path 2 3from . import views 4 5app_name = "thread" 6 7urlpatterns = [ 8 path("create_topic/", views.TopicCreateView.as_view(), name="create_topic"), 9 ]
views
1from django.shortcuts import render, redirect, get_object_or_404 2from django.views.generic import CreateView 3from django.urls import reverse_lazy 4from .forms import TopicCreateForm, TopicModelForm, CommentModelForm 5from .models import Topic, Category, Comment 6 7class TopicCreateView(CreateView): 8 template_name = "thread/create_topic.html" 9 form_class = TopicModelForm 10 model = Topic 11 success_url = reverse_lazy("base:top") 12 13 def form_valid(self, form): 14 ctx = {"form": form} 15 if self.request.POST.get("next", "") == "confirm": 16 return render(self.request, "thread/confirm_topic.html", ctx) 17 if self.request.POST.get("next", "") == "back": 18 return render(self.request, "thread/create_topic.html", ctx) 19 if self.request.POST.get("next", "") == "create": 20 return super().form_valid(form) 21 else: 22 # 正常動作ではここは通らない。エラーページへの遷移でも良い 23 return redirect(reverse_lazy("base:top")) 24 25 26def topic_create(request): 27 template_name = "thread/create_topic.html" 28 ctx = {} 29 if request.method == "GET": 30 ctx["form"] = TopicCreateForm() 31 return render(request, template_name, ctx) 32 33 if request.method == "POST": 34 topic_form = TopicCreateForm(request.POST) 35 if topic_form.is_valid(): 36 topic_form.save() 37 return redirect(reverse_lazy("base:top")) 38 else: 39 ctx["form"] = topic_form 40 return render(request, template_name, ctx) 41
forms
1from django import forms 2from .models import Topic, Category, Comment 3 4 5class TopicModelForm(forms.ModelForm): 6 class Meta: 7 model = Topic 8 fields = [ 9 "title", 10 "user_name", 11 "category", 12 "message", 13 ] 14 widgets = { 15 "title": forms.TextInput(attrs={"class": "hoge"}), 16 "user_name": forms.TextInput(attrs={"value": "名無し"}), 17 } 18 19 def __init__(self, *args, **kwargs): 20 # kwargs.setdefault('label_suffix', '') 21 super().__init__(*args, **kwargs) 22 self.fields["category"].empty_label = "選択して下さい" 23 self.fields["user_name"].widget.attrs["value"] = "匿名" 24 # self.fields['title'].widget.attrs['class'] = 'huga' 25 26 27class TopicCreateForm(forms.Form): 28 title = forms.CharField( 29 label="タイトル", 30 max_length=255, 31 required=True, 32 ) 33 user_name = forms.CharField( 34 label="お名前", 35 max_length=30, 36 required=True, 37 widget=forms.TextInput(attrs={"value": "名無し"}), 38 ) 39 category = forms.ModelChoiceField( 40 label="カテゴリー", 41 queryset=Category.objects.all(), 42 required=True, 43 empty_label="選択して下さい", 44 ) 45 message = forms.CharField( 46 label="本文", 47 widget=forms.Textarea, 48 required=True, 49 ) 50 51 def __init__(self, *args, **kwargs): 52 kwargs.setdefault("label_suffix", "") 53 super().__init__(*args, **kwargs) 54 55 56class CommentModelForm(forms.ModelForm): 57 class Meta: 58 model = Comment 59 fields = [ 60 "user_name", 61 "message", 62 # "image", 63 ] 64 65 def __init__(self, *args, **kwargs): 66 kwargs.setdefault("label_suffix", "") 67 super().__init__(*args, **kwargs) 68 self.fields["user_name"].widget.attrs["value"] = "名無し" 69 70 def save_with_topic(self, topic_id, commit=True): 71 comment = self.save(commit=False) 72 comment.topic = Topic.objects.get(id=topic_id) 73 comment.no = Comment.objects.filter(topic_id=topic_id).count() + 1 74 if commit: 75 comment.save() 76 return comment
basehtml
1{% load static %} 2<!DOCTYPE html> 3 4<head> 5 <meta charset="UTF-8"> 6 <meta http-equiv="content-language" content="ja"> 7 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 8 {% block meta_tag %}{% endblock %} 9 <link href="{% static 'css/semantic.css' %}" rel="stylesheet"> 10 {% block css %}{% endblock %} 11 <title> 12 {% block title %}掲示板{% endblock %} 13 </title> 14</head> 15 16<body> 17 <div class="ui stackable inverted menu"> 18 <a href="{% url 'base:top' %}" class="header item"> 19 掲示板 20 </a> 21 <a href="{% url 'base:about' %}" class="item"> 22 このサイトは何? 23 </a> 24 <a class="item" href="{% url 'thread:create_topic' %}"> 25 投稿する 26 </a> 27 <div class="right menu"> 28 <a class="item"> 29 Log in 30 </a> 31 <a class="item"> 32 Sign up 33 </a> 34 </div> 35 </div> 36 37 <div class="ui container" style="min-height:100vh;"> 38 {% block content %} 39 {% endblock %} 40 </div> 41 <div class="ui inverted stackable footer segment"> 42 <div class="ui container center aligned"> 43 <div class="ui horizontal inverted small divided link list"> 44 <a href="{% url 'base:top' %}" class="item">あ</a> 45 <a href="{% url 'base:terms' %}" class="item">い</a> 46 <a href="{% url 'base:policy' %}" class="item">う</a> 47 </div> 48 </div> 49 </div> 50 <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> 51 <script type="text/javascript" src="{% static 'js/semantic.js' %}"></script> 52 {% block js %}{% endblock %} 53</body>
試したこと
pathを以下のものに変えてみましたが同じエラーが出ます。
path("create_topic/", views.topic_create, name="create_topic"),
path("create_topic/str:url_code/", views.TopicCreateView.as_view(), name="create_topic"),
補足情報(FW/ツールのバージョンなど)
Python3.8.5
Django3.1.4
Windows10
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。