質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

1153閲覧

Django Error内容が分からない

退会済みユーザー

退会済みユーザー

総合スコア0

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/01/11 11:26

編集2020/01/11 13:36

前提・実現したいこと

Djangoにおいて発生したエラーを解消したい
該当する箇所がどこなのか分からず困っています
どなたかご教授頂けると幸いです
http://127.0.0.1:8000/polls/でエラーが発生

発生している問題・エラーメッセージ

error

1TypeError at /polls/ 2__init__() takes 1 positional argument but 2 were given 3Request Method: GET 4Request URL: http://127.0.0.1:8000/polls/ 5Django Version: 3.0.1 6Exception Type: TypeError 7Exception Value: 8__init__() takes 1 positional argument but 2 were given 9Exception Location: C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py in _get_response, line 113 10Python Executable: C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\python.exe 11Python Version: 3.8.0 12Python Path: 13['C:\Users\sa96t\Desktop\Django\mysite', 14 'C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\python38.zip', 15 'C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\DLLs', 16 'C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\lib', 17 'C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32', 18 'C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\lib\site-packages'] 19Server time: Sat, 11 Jan 2020 11:17:02 +0000 20 21Traceback Switch to copy-and-paste view 22C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py in inner 23 response = get_response(request) … 24▶ Local vars 25C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py in _get_response 26 response = self.process_exception_by_middleware(e, request) … 27▶ Local vars 28C:\Users\sa96t\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py in _get_response 29 response = wrapped_callback(request, *callback_args, **callback_kwargs) … 30▶ Local vars

該当のソースコード

該当する箇所が分からないが、該当しそうな箇所を記述しようと思います

polls/urls.py

1from django.urls import path 2 3from . import views 4 5app_name = 'polls' 6 7urlpatterns = [ 8 #path('', views.index, name='index'), 9 #path('<int:question_id>/', views.detail, name='detail'), 10 #path('<int:question_id>/result', views.results, name='result'), 11 path('<int:question_id>/vote/', views.vote, name='vote'), 12 13 path('', views.IndexView, name='index'), 14 path('<int:pk>/', views.DetailView.as_view(), name='detail'), 15 path('<int:pk>/results/', views.ResultsView.as_view(), name='results'), 16]

polls/views.py

1def vote(request, question_id): 2 return HttpResponse("You're voting on question %s." % question_id) 3 4class IndexView(generic.ListView): 5 template_name = 'polls/index.html' 6 context_object_name = 'latest_question_list' 7 8 def get_queryset(self): 9 #return Question.objects.order_by('-pub_date')[:5] 10 11 return Question.objects.filter( 12 pub_date__lte = timezone.now() 13 ).order_by('-pub_date')[:5] 14 15class DetailView(generic.DetailView): 16 model = Question 17 template_name = 'polls/detail.html' 18 19 def get_queryset(self): 20 return Question.objects.filter(pub_date__lte=timezone.now()) 21 22class ResultsView(generic.DetailView): 23 model = Question 24 template_name = 'polls/results.html'

polls/templates/polls/index.html

1{% load static %} 2 3<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}"> 4 5{% if latest_question_list %} 6 <ul> 7 {% for question in latest_question_list %} 8 <li><a href="{% url 'polls:detail' question_id %}">{{ question.question_text }}</a>></li> 9 {% endfor %} 10 </ul> 11{% else %} 12 <p>No polls are available</p> 13{% endif %}

試したこと

http://127.0.0.1:8000/polls/0/ -> Page Not Found
http://127.0.0.1:8000/polls/1/ -> 表示可能

http://127.0.0.1:8000/polls/0/results/ -> Page Not Found
http://127.0.0.1:8000/polls/1/results/ -> 表示可能

補足情報(FW/ツールのバージョンなど)

以下のサイトを初めから実践しており、その6まで
参考資料

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

python

1path('', views.IndexView, name='index'),

の部分as_view()がないですね

投稿2020/01/11 13:59

mistn

総合スコア1191

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/01/12 01:21

回答の程、ありがとうございました。
guest

0

コード貼ってないため、具体的な箇所指定できませんが
親classを継承する際にselfが抜けてるか、もしくは必要以上にパラメータを与えてしまったのです
init()辺りを確認してください

投稿2020/01/11 12:42

編集2020/01/11 12:44
harinezumi.py

総合スコア282

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/01/11 13:42

回答の程、ありがとうございます。 個人的に、該当しそうなところのコードを追加させて頂きました。(ご指摘の程、ありがとうございます) まだ、Django初心者なので、あまりよく分かっていないのですが、 selfはviews.pyを見る限り抜けていないと思っており、init()の場所はどこにあるのか分かりませんでした。
harinezumi.py

2020/01/11 15:15

path('', views.IndexView, name='index'), as_view()が抜けてました
退会済みユーザー

退会済みユーザー

2020/01/12 01:21

回答の程、ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問