質問編集履歴
5
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -153,7 +153,7 @@
|
|
153
153
|
---
|
154
154
|
---
|
155
155
|
http://127.0.0.1:8000/app/app/new で追加ページを表示することができました。
|
156
|
-
しかし、今回は http://127.0.0.1:8000/admin/app/new で追加ページを表示したいので、参考にするサイトをこちらに変更してコードを書き直しました。
|
156
|
+
しかし、今回は http://127.0.0.1:8000/admin/app/new で追加ページを表示したいので、参考にするサイトをこちらに(https://tsukasa-blog.com/programming/django-admin-add-url/ )変更してコードを書き直しました。
|
157
157
|
|
158
158
|
画像1枚目のようにhttp://127.0.0.1:8000/admin/app/ に追加ページのリンクのサイトを追加し、画像2枚目のように http://127.0.0.1:8000/admin/app/new を表示したいです。
|
159
159
|
|
4
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -149,6 +149,205 @@
|
|
149
149
|
|
150
150
|
new.html を変更してみたが変わらず Not Found をサイトに表示された。
|
151
151
|
|
152
|
+
**追記**
|
153
|
+
---
|
154
|
+
---
|
155
|
+
http://127.0.0.1:8000/app/app/new で追加ページを表示することができました。
|
156
|
+
しかし、今回は http://127.0.0.1:8000/admin/app/new で追加ページを表示したいので、参考にするサイトをこちらに変更してコードを書き直しました。
|
157
|
+
|
158
|
+
画像1枚目のようにhttp://127.0.0.1:8000/admin/app/ に追加ページのリンクのサイトを追加し、画像2枚目のように http://127.0.0.1:8000/admin/app/new を表示したいです。
|
159
|
+
|
160
|
+

|
161
|
+
|
162
|
+
|
163
|
+

|
164
|
+
|
165
|
+
|
166
|
+
**変更したディレクトリ構造**
|
167
|
+
---
|
168
|
+
```ここに言語を入力
|
169
|
+
app
|
170
|
+
├─urls.py
|
171
|
+
├─views.py
|
172
|
+
├─admin.py
|
173
|
+
└─templates
|
174
|
+
├─admin
|
175
|
+
│ └─app
|
176
|
+
│ ├─new.html
|
177
|
+
│ └─change_list.html
|
178
|
+
│
|
179
|
+
└─app
|
180
|
+
├─detail.html
|
181
|
+
├─index.html
|
182
|
+
└─results.html
|
183
|
+
```
|
184
|
+
|
185
|
+
### 発生している問題・エラーメッセージ
|
186
|
+
|
187
|
+
```
|
188
|
+
return new_urls + http://127.0.0.1:8000/admin/app/new
|
189
|
+
```
|
190
|
+
|
191
|
+
**該当のソースコード**
|
192
|
+
---
|
193
|
+
urls.py
|
194
|
+
```ここに言語を入力
|
195
|
+
from django.urls import path
|
196
|
+
|
197
|
+
from . import views
|
198
|
+
|
199
|
+
app_name = 'app'
|
200
|
+
urlpatterns = [
|
201
|
+
path('', views.IndexView.as_view(), name='index'),
|
202
|
+
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
|
203
|
+
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
|
204
|
+
path('<int:question_id>/vote/', views.vote, name='vote'),
|
205
|
+
]
|
206
|
+
```
|
207
|
+
views.py
|
208
|
+
```ここに言語を入力
|
209
|
+
from django.http import HttpResponseRedirect
|
210
|
+
from django.shortcuts import get_object_or_404, render
|
211
|
+
from django.urls import reverse
|
212
|
+
from django.views import generic
|
213
|
+
|
214
|
+
from .models import Choice, Question
|
215
|
+
|
216
|
+
|
217
|
+
class IndexView(generic.ListView):
|
218
|
+
template_name = 'app/index.html'
|
219
|
+
context_object_name = 'latest_question_list'
|
220
|
+
|
221
|
+
def get_queryset(self):
|
222
|
+
"""Return the last five published questions."""
|
223
|
+
return Question.objects.order_by('-pub_date')[:5]
|
224
|
+
|
225
|
+
|
226
|
+
class DetailView(generic.DetailView):
|
227
|
+
model = Question
|
228
|
+
template_name = 'app/detail.html'
|
229
|
+
|
230
|
+
|
231
|
+
class ResultsView(generic.DetailView):
|
232
|
+
model = Question
|
233
|
+
template_name = 'app/results.html'
|
234
|
+
|
235
|
+
|
236
|
+
def vote(request, question_id):
|
237
|
+
question = get_object_or_404(Question, pk=question_id)
|
238
|
+
try:
|
239
|
+
selected_choice = question.choice_set.get(pk=request.POST['choice'])
|
240
|
+
except (KeyError, Choice.DoesNotExist):
|
241
|
+
# Redisplay the question voting form.
|
242
|
+
return render(request, 'app/detail.html', {
|
243
|
+
'question': question,
|
244
|
+
'error_message': "You didn't select a choice.",
|
245
|
+
})
|
246
|
+
else:
|
247
|
+
selected_choice.votes += 1
|
248
|
+
selected_choice.save()
|
249
|
+
# Always return an HttpResponseRedirect after successfully dealing
|
250
|
+
# with POST data. This prevents data from being posted twice if a
|
251
|
+
# user hits the Back button.
|
252
|
+
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
|
253
|
+
```
|
254
|
+
admin.py
|
255
|
+
```ここに言語を入力
|
256
|
+
from django.contrib import admin
|
257
|
+
|
258
|
+
from .models import Choice, Question
|
259
|
+
|
260
|
+
|
261
|
+
class ChoiceInline(admin.TabularInline):
|
262
|
+
model = Choice
|
263
|
+
extra = 3
|
264
|
+
|
265
|
+
|
266
|
+
class QuestionAdmin(admin.ModelAdmin):
|
267
|
+
fieldsets = [
|
268
|
+
(None, {'fields': ['question_text']}),
|
269
|
+
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
|
270
|
+
]
|
271
|
+
inlines = [ChoiceInline]
|
272
|
+
list_display = ('question_text', 'pub_date', 'was_published_recently')
|
273
|
+
list_filter = ['pub_date']
|
274
|
+
search_fields = ['question_text']
|
275
|
+
|
276
|
+
actions = ['change_title_action']
|
277
|
+
|
278
|
+
|
279
|
+
@admin.register
|
280
|
+
def get_urls(self):
|
281
|
+
urls = super().get_urls()
|
282
|
+
new_urls = [
|
283
|
+
path('new/', self.admin_site.admin_view(self.add_view), name="new_page"),
|
284
|
+
]
|
285
|
+
return new_urls + http://127.0.0.1:8000/admin/app/new
|
286
|
+
|
287
|
+
def new_view(self, request):
|
288
|
+
return TemplateResponse(request, "admin/app/new.html")
|
289
|
+
|
290
|
+
|
291
|
+
admin.site.register(Question, QuestionAdmin)
|
292
|
+
```
|
293
|
+
|
294
|
+
new.html
|
295
|
+
```ここに言語を入力
|
296
|
+
{% extends "admin/index.html" %}
|
297
|
+
{% block content %}
|
298
|
+
追加ページ
|
299
|
+
{% endblock %}
|
300
|
+
```
|
301
|
+
|
302
|
+
change_list.html
|
303
|
+
```ここに言語を入力
|
304
|
+
{% block content %}
|
305
|
+
<span><</span><span>a </span><span>href</span><span>=</span>"/admin/app/book/add_page">追加ページへ<span><</span><span>/a></span>
|
306
|
+
{% endblock %}
|
307
|
+
```
|
308
|
+
detail.html
|
309
|
+
```ここに言語を入力
|
310
|
+
<h1>{{ question.question_text }}</h1>
|
311
|
+
|
312
|
+
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
|
313
|
+
|
314
|
+
<form action="{% url 'app:vote' question.id %}" method="post">
|
315
|
+
{% csrf_token %}
|
316
|
+
{% for choice in question.choice_set.all %}
|
317
|
+
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
|
318
|
+
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
|
319
|
+
{% endfor %}
|
320
|
+
<input type="submit" value="Vote">
|
321
|
+
</form>
|
322
|
+
```
|
323
|
+
|
324
|
+
index.html
|
325
|
+
```ここに言語を入力
|
326
|
+
{% if latest_question_list %}
|
327
|
+
<ul>
|
328
|
+
{% for question in latest_question_list %}
|
329
|
+
<li><a href="{% url 'app:detail' question.id %}">{{ question.question_text }}</a></li>
|
330
|
+
{% endfor %}
|
331
|
+
</ul>
|
332
|
+
{% else %}
|
333
|
+
<p>No app are available.</p>
|
334
|
+
{% endif %}
|
335
|
+
```
|
336
|
+
|
337
|
+
results.html
|
338
|
+
```ここに言語を入力
|
339
|
+
<h1>{{ question.question_text }}</h1>
|
340
|
+
|
341
|
+
<ul>
|
342
|
+
{% for choice in question.choice_set.all %}
|
343
|
+
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
|
344
|
+
{% endfor %}
|
345
|
+
</ul>
|
346
|
+
|
347
|
+
<a href="{% url 'app:detail' question.id %}">Vote again?</a>
|
348
|
+
```
|
349
|
+
|
350
|
+
|
152
351
|
### 補足情報(FW/ツールのバージョンなど)
|
153
352
|
|
154
353
|
Django 4.0.6
|
3
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -37,7 +37,7 @@
|
|
37
37
|
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
|
38
38
|
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
|
39
39
|
path('<int:question_id>/vote/', views.vote, name='vote'),
|
40
|
-
path('p
|
40
|
+
path('app/new/', views.new, name='new),
|
41
41
|
]
|
42
42
|
```
|
43
43
|
|
2
誤字
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
【Django】 作成したサイトの追加ページが Not Found で表示されない
|
1
|
+
【Django】 作成したサイトの追加ページが Not Found で表示されない
|
test
CHANGED
File without changes
|
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
### 前提
|
2
2
|
|
3
3
|
機械学習初心者です。
|
4
|
-
こちらのサイト(https://genchan.net/it/programming/python/django/14304/ )を参考に、サイトにhttp://127.0.0.1:8000/admin/app/new という追加ページを作成したのですが、アクセスしたところ Not Found と返ってきてしまいました。
|
4
|
+
こちらのサイト(https://genchan.net/it/programming/python/django/14304/ )を参考に、作成したサイトに http://127.0.0.1:8000/admin/app/new という追加ページを作成したのですが、アクセスしたところ Not Found と返ってきてしまいました。
|
5
5
|
初歩的な質問で恐縮ですが、ご教授頂けると幸いです。
|
6
6
|
|
7
7
|
### 実現したいこと
|