DjangoにてTemplateViewを使って、サイト構築中です。
Createviewで、Reverselazy後に設定したパターンネームが違っているのかエラーが出ます。
データに書き込まれているので、データ送信までは確認できました。
その後、画面遷移でエラーが起きています。
appname:indexとしていますが、"逆引き名が違う"と遷移先に行きません。
Python
1エラー文 2 3NoReverseMatch at /post_create/ 4Reverse for 'index' not found. 'index' is not a valid view function or pattern name. 5Request Method: POST 6Request URL: http://127.0.0.1:8000/post_create/ 7Django Version: 3.0.4 8Exception Type: NoReverseMatch 9Exception Value: 10Reverse for 'index' not found. 'index' is not a valid view function or pattern name.
Python
1base.html 2 3{% load static %} 4<!DOCTYPE html> 5<html lang="ja"> 6<head> 7 <meta charset="UTF-8"> 8 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 9 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 10 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous"> 11 <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script> 12 <link rel="stylesheet" type="text/css" href="{% static 'app/style.css' %}"> 13</head> 14<body> 15 <nav class="navbar navbar-expand-lg navbar-light bg-light"> 16 <div class="container-fluid"> 17 <a class="navbar-brand" href="/">Home</a> 18 <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation"> 19 <span class="navbar-toggler-icon"></span> 20 </button> 21 <div class="collapse navbar-collapse" id="navbarNavAltMarkup"> 22 <div class="navbar-nav"> 23 <a class="nav-link active" aria-current="page" href="{% url 'app:post_create' %}">Tweet</a> 24 25 </div> 26 </div> 27 </div> 28 </nav> 29 30 <div class="container"> 31 32 <div class="card"> 33 {% block content %} 34 {% endblock %} 35 </div> 36 </div> 37 </div> 38</div> 39</body> 40</html> 41
Python
1index.html 2 3{% extends 'app/base.html' %} 4{% load static %} 5 6 7{% block content %} 8<h1 style="text-align:center; color:white; background-color:skyblue;">{% block title %}Time_Line_Site{% endblock%}</h1> 9 {% for tweet in tweet_list %} 10 <div class="row"> 11 <div class="col"> 12 <div class="cardbody"> 13 <div class="card-title"><strong>@{{tweet.name}}</strong></span> 14 <span>{{tweet.created_at}}</span> 15 <div class="mx-auto" style="width: 500px;"> 16 <div class= "card-text">{{ tweet.tweet|linebreaksbr }}</div> 17 </div> 18 <div class="border-bottom"></div> 19 </div> 20 </div> 21 </div> 22 23 {% endfor %} 24{% endblock%} 25 26 27
Python
1post_create.html 2 3{% extends 'app/base.html' %} 4{% load static %} 5 6 7{% block content %} 8 <form action="" method="post"> 9 {% csrf_token %} 10 {{ form.as_p }} 11 <div class="input-group"> 12 <button type="submit" class="btn btn-primary" >Add New</button> 13 </div> 14 </form> 15 {% endblock%} 16
Python
1urls.py 2 3from django.urls import path 4 5from .views import IndexView, PostCreateView 6from . import views 7app_name = "app" 8 9urlpatterns = [ 10 path("",IndexView.as_view(template_name= "app/index.html")), 11 path("post_create/",PostCreateView.as_view(),name="post_create"), 12] 13
Python
1views.py 2 3from django.contrib.messages.api import success 4from django.views.generic import ListView,TemplateView, CreateView 5from django.shortcuts import render 6from .models import User 7from django.contrib import messages 8from django.urls import reverse_lazy 9 10 11class IndexView(ListView): 12 model = User 13 template_name = "index.html" 14 context_object_name = 'tweet_list' 15 paginate_by = 10 16 17 18class PostCreateView(CreateView): 19 model = User 20 template_name = "app/post_create.html" 21 fields = '__all__' 22 success_url = reverse_lazy("app:index") 23 24
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。