Djangoでpost_createのurlをつけたところ、見つからないとエラーが出ます。
TemplateDoesNotExist at /post_create/
となります。
記事や資料を見て、試したのですが、解決できません。
下記エラーです
Python
1TemplateDoesNotExist at /post_create/ 2post_create 3Request Method: GET 4Request URL: http://127.0.0.1:8000/post_create/ 5Django Version: 3.0.4 6Exception Type: TemplateDoesNotExist 7Exception Value: 8post_create 9Exception Location: C:\Users\yasut\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader.py in select_template, line 47 10 11Exception Value: 12Reverse for 'post_create' not found. 'post_create' is not a valid view function or pattern name. 13Exception Location: C:\Users****\AppData\Local\Programs\Python\Python37\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 677 14Python Executable: C:\Users****\AppData\Local\Programs\Python\Python37\python.exe 15Python Version: 3.7.4 16Python Path: 17['C:\twitter_site', 18 'C:\Users\****\AppData\Local\Programs\Python\Python37\python37.zip', 19 'C:\Users\****\AppData\Local\Programs\Python\Python37\DLLs', 20 'C:\Users\****\AppData\Local\Programs\Python\Python37\lib', 21 'C:\Users\****\AppData\Local\Programs\Python\Python37', 22 'C:\Users\****\AppData\Roaming\Python\Python37\site-packages', 23 'C:\Users\****\AppData\Local\Programs\Python\Python37\lib\site-packages', 24 'C:\Users\****\AppData\Local\Programs\Python\Python37\lib\site-packages\win32', 25 'C:\Users\****\AppData\Local\Programs\Python\Python37\lib\site-packages\win32\lib', 26 'C:\Users\****\AppData\Local\Programs\Python\Python37\lib\site-packages\Pythonwin'] 27Server time: 水, 1 12月 2021 22:38:41 +0900 28Error during template rendering 29In template C:\twitter_site\app\templates\app\base.html, error at line 21 30 31Reverse for 'post_create' not found. 'post_create' is not a valid view function or pattern name. 3211 </head> 3312 <body> 3413 <div class= "container" style="text-align:center"> 3514 <h1 style="text-align:center; color:white; background-color:skyblue;">{% block title %}Time_Line_Site{% endblock%}</h1> 3615 3716 {% block content %} 3817 3918 {% endblock %} 4019 </div> 4120 4221 <a href="{% url 'post_create' %}" button type="button"style="text-align:center" class="btn btn-outline-primary">Tweet</button> 4322 4423 4524 </body> 4625 </html> 4726
HTML
1Base.html 2{% load static %} 3<!DOCTYPE html> 4<html lang="ja"> 5<head> 6 <meta charset="UTF-8"> 7 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 8 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 9 <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"> 10 <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> 11 <link rel="stylesheet" type="text/css" href="{% static 'app/style.css' %}"> 12</head> 13<body> 14 <div class= "container" style="text-align:center"> 15 <h1 style="text-align:center; color:white; background-color:skyblue;">{% block title %}Time_Line_Site{% endblock%}</h1> 16 17 {% block content %} 18 19 {% endblock %} 20 </div> 21 22 <a href="post_create" button type="button"style="text-align:center" class="btn btn-outline-primary">Tweet</button> 23 24 25 </body> 26</html>
html
1index.html 2 3{% extends 'app/base.html' %} 4{% load static %} 5 6 7{% block content %} 8<div class="balloon1-left"> 9 10 {% for tweet in tweet_list %} 11 12 <h4>{{tweet.name}}</h4> 13 <p>{{tweet.tweet}}</p> 14 <p>{{tweet.created_at}}</p> 15 <p class="border-bottom"></p> 16 17{% endfor %} 18</div> 19{% endblock%} 20
HTML
1post_create.html 2{% extends 'app/base.html' %} 3 4 5{% block content %} 6{{forms.as_p}} 7 <form> 8 {% csrf_token %} 9 <div class="input-group"> 10 <input type="text" class="form-control" placeholder="ツイート" ng-model=""> 11 <span class="input-group-btn"> 12 <button type="submit" class="btn btn-primary" ng-click="addThing()">Add New</button> 13 </span> 14 </div> 15 </form> 16 {% endblock%} 17 18
Python
1urls.py 2 3from django.urls import path 4 5from .views import IndexView, PostCreateView 6 7 8urlpatterns = [ 9 path("",IndexView.as_view(template_name= "app/index.html")), 10 path("post_create",PostCreateView.as_view(template_name="post_create")), 11] 12
Python
1 2from django.views.generic import ListView,TemplateView, CreateView 3from .models import User 4 5 6class IndexView(ListView): 7 model = User 8 template_name = "index.html" 9 context_object_name = 'tweet_list' 10 paginate_by = 10 11 12class PostCreateView(CreateView): 13 model = User 14 template_name = "post_create" 15 fields = ['tweet'] 16 17 success_url = '/' 18
ルート直下のurls.py
Python
1from django.contrib import admin 2from django.urls import path 3from django.urls.conf import include 4 5urlpatterns = [ 6 path('', include('app.urls')), 7 path('admin/', admin.site.urls), 8] 9
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/12/03 12:50