質問編集履歴

1

changed codes

2022/12/27 12:39

投稿

alizona
alizona

スコア126

test CHANGED
File without changes
test CHANGED
@@ -1,12 +1,164 @@
1
- djangoでログイン機能とログインした後に object_list を取得して表示するプログラムを書いています。
1
+ djangoでログインした後に object_list の投稿一覧と、投稿追加用のフォームを表示するプログラムを書いています。
2
-
3
- 127.0.0.1:8000/index/ と検索すると object_list が出力されるのですが、
2
+
4
- 127.0.0.1:8000 と検索すると object_listの中身が 表示されません。
5
-
6
- どちらにも同じ中身の htmlファイルを別のfile 名で設定しています。
7
3
 
8
4
  どうすれば、login直後に 127.0.0.1:8000/で objet_listを表示できるのでしょうか?
9
5
 
6
+ 投稿追加フォームの一部のみが表示されます。
7
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-12-27/cd5fe02a-1ed2-4f99-9cfc-090c6d73a931.png)
8
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-12-27/6f81cccb-cc6b-4baa-9651-fa4faaac5ce5.png)
9
+ ### 現在のコード
10
+ ```python
11
+ #project/urls.py
12
+ from django.contrib import admin
13
+ from django.urls import path, include
14
+
15
+ urlpatterns = [
16
+ path("admin/", admin.site.urls),
17
+ path('',include('registration.urls')),
18
+ ]
19
+ ```
20
+ ```python
21
+ #app(registration)/urls.py
22
+ from django.urls import path
23
+
24
+ from . import views
25
+ from registration.views import post_detail
26
+
27
+ from django.urls import path, include
28
+ from django.contrib.auth.decorators import login_required
29
+ from django.views.generic import TemplateView
30
+ index_view = TemplateView.as_view(template_name="registration/frontpage.html")
31
+
32
+ urlpatterns=[
33
+ path("", login_required(index_view), name="frontpage"),
34
+ # この1行でdjangoでデフォルトで用意している以下がすべて入ります。
35
+ # ・ログイン
36
+ # ・ログアウト
37
+ # ・パスワード変更
38
+ # ・パスワード再発行
39
+ path('', include("django.contrib.auth.urls")),
40
+ path("<int:pk>/", post_detail, name="post_detail"),
41
+ path('update/<pk>/', views.Update.as_view(), name="update"),
42
+ path('delete/<pk>/', views.Delete.as_view(), name="delete"),
43
+ ]
44
+ ```
45
+
46
+ ```python
47
+ #templates/registration/frontpage.html
48
+ {% extends "registration/base.html" %}
49
+ {% block content %}
50
+
51
+ <h2 class="subtitle">add post</h2>
52
+ <form action="." method="post">
53
+ {% csrf_token%}
54
+ {{form.as_p}}
55
+ <div class="field">
56
+ <div class="control">
57
+ <button class="button is-danger">submit</button>
58
+ </div>
59
+ </div>
60
+ </form>
61
+ <br>
62
+ <hr>
63
+
64
+ {% for post in posts %}
65
+ <!------------------post placed------------->
66
+ <div class="post block">
67
+ <h2 class="subtitle">{{post.title}}</h2>
68
+ Due:&nbsp;&nbsp;{{post.due}}
69
+ <br>
70
+ {{post.fromwho}}
71
+ &nbsp;->&nbsp;
72
+ {{post.towho}}
73
+ <br>
74
+ <p>{{post.body}}</p>
75
+ <small>Assigned:&nbsp;&nbsp;{{post.posted_date}}</small>
76
+ <br>
77
+ <a href="{% url 'update' post.pk %}">edit task</a>&nbsp;&nbsp;&nbsp;
78
+ <a href="{% url 'delete' post.pk %}">delete task</a>
79
+ </div>
80
+
81
+ <!--------------comment placed---------------->
82
+ {% for comment in post.comments.all %}
83
+ <article class = "media">
84
+ <div class="media_content">
85
+ <div class="content">
86
+ <p>
87
+ <strong>
88
+ &nbsp;&nbsp;&nbsp;&nbsp;{{comment.name}}
89
+ &nbsp;&nbsp;&nbsp;&nbsp;<small>{{comment.posted_date}}</small>
90
+ </strong>
91
+ <br/>
92
+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{comment.body}}
93
+ </p>
94
+ </div>
95
+ </div>
96
+ </article>
97
+ <br>
98
+
99
+ {% empty %}
100
+ <div class="notification">
101
+ <p>comment is not yet.</p>
102
+ </div>
103
+ <br>
104
+ {% endfor %}
105
+
106
+ &nbsp;&nbsp;&nbsp;&nbsp;<a href="{% url 'post_detail' post.pk %}">add comment</a>
107
+ <br>
108
+ <br>
109
+ <hr>
110
+
111
+ {% endfor %}
112
+ {% endblock %}
113
+ ```
114
+ ```python
115
+ #views.py
116
+ # Create your views here.
117
+ from django.shortcuts import render,redirect
118
+ from registration.forms import CommentForm, PostForm
119
+ from .models import Post
120
+
121
+
122
+ def frontpage(request):
123
+ posts = Post.objects.all()
124
+
125
+ if request.method=="POST":
126
+ form=PostForm(request.POST)
127
+
128
+ if form.is_valid():
129
+ comment=form.save(commit=False)
130
+ comment.save()
131
+ return redirect("frontpage")
132
+ else:
133
+ form =PostForm()
134
+ return render(request, "registration/frontpage.html", {"posts":posts, "form":form})
135
+
136
+
137
+
138
+ # add comment のため。
139
+ def post_detail(request, pk):
140
+ post = Post.objects.get(pk=pk)
141
+
142
+ if request.method=="POST":
143
+ form=CommentForm(request.POST)
144
+
145
+ if form.is_valid():
146
+ comment=form.save(commit=False)
147
+ comment.post=post
148
+ comment.save()
149
+ return redirect("post_detail",pk=post.pk)
150
+ else:
151
+ form =CommentForm()
152
+
153
+ return render(request, "registration/post_detail.html",{"post":post, "form":form})
154
+
155
+
156
+ ```
157
+
158
+ 127.0.0.1:8000/1/ としたところ、post_detail.htmlがコメント追加フォームと一緒に問題なく表示されています。
159
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-12-27/62ef6039-134a-4536-b8ec-5b0641ecfd80.png)
160
+
161
+ ### 過去のコード
10
162
  ```python
11
163
  #project/urls.py
12
164