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

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

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

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

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

2回答

4009閲覧

Djangoのログイン実装でTypeError: __init__() takes 1 positional argument but 2 were givenが発生してしまう

0ae

総合スコア13

Django

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

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/07/28 14:22

編集2019/07/28 14:28

前提・実現したいこと

DjangoでTwitterログインを実装しようとしています。
こちらの記事を参考に実装を進めましたがエラーが発生しています。
バージョンは
Django = 2.0.6
python = 3.6.8
social-auth-app-django = 2.9.1
となっています。

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

sudo python3 manage.py runserver 0.0.0.0:8000でサーバーの起動はできるのですがログインページにアクセスしようとすると以下のようなエラーが発生してしまいます。

[28/Jul/2019 22:31:41] "GET /user/login/ HTTP/1.1" 500 58091 Internal Server Error: /user/login/ Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 35, in inner response = get_response(request) File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 128, in _get_response response = self.process_exception_by_middleware(e, request) File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 126, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) TypeError: __init__() takes 1 positional argument but 2 were given

該当のソースコード

ディレクトリの構造はこのようになっています。

. ├── db.sqlite3 ├── manage.py ├── spmove │   ├── __init__.py │   ├── __init__.pyc │   ├── __pycache__ │   │   ├── __init__.cpython-36.pyc │   │   ├── settings.cpython-36.pyc │   │   ├── urls.cpython-36.pyc │   │   └── wsgi.cpython-36.pyc │   ├── settings.py │   ├── settings.pyc │   ├── urls.py │   ├── urls.pyc │   └── wsgi.py └── user_auth ├── __init__.py ├── __init__.pyc ├── __pycache__ │   ├── __init__.cpython-36.pyc │   ├── admin.cpython-36.pyc │   ├── models.cpython-36.pyc │   ├── urls.cpython-36.pyc │   └── views.cpython-36.pyc ├── admin.py ├── admin.pyc ├── apps.py ├── migrations │   ├── __init__.py │   └── __pycache__ │   └── __init__.cpython-36.pyc ├── models.py ├── models.pyc ├── templates │   └── user_auth │   ├── login.html │   ├── logout.html │   └── top.html ├── tests.py ├── urls.py └── views.py

(一部文字列制限のため...で省略させて頂いています)
/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py

python:

1import logging 2import types 3 4from django.conf import settings 5from django.core.exceptions import ImproperlyConfigured, MiddlewareNotUsed 6from django.db import connections, transaction 7from django.urls import get_resolver, set_urlconf 8from django.utils.module_loading import import_string 9 10from .exception import convert_exception_to_response, get_exception_response 11 12...(文字列制限のため省略させていただきます...) 13 14non_atomic_requests: 15 view = transaction.atomic(using=db.alias)(view) 16 return view 17 18 def get_exception_response(self, request, resolver, status_code, exception): 19 return get_exception_response(request, resolver, status_code, exception, self.__class__) 20 21 def get_response(self, request): 22 """Return an HttpResponse object for the given HttpRequest.""" 23 # Setup default url resolver for this thread 24 set_urlconf(settings.ROOT_URLCONF) 25 26 response = self._middleware_chain(request) 27 28 response._closable_objects.append(request) 29 if not getattr(response, 'is_rendered', True) and callable(getattr(response, 'render', None)): 30 response = response.render() 31 32 if response.status_code == 404: 33 logger.warning( 34 'Not Found: %s', request.path, 35 extra={'status_code': 404, 'request': request}, 36 ) 37 38 return response 39 40 ... 41 42 43 elif hasattr(response, 'render') and callable(response.render): 44 for middleware_method in self._template_response_middleware: 45 response = middleware_method(request, response) 46 47 if response is None: 48 raise ValueError( 49 "%s.process_template_response didn't return an " 50 "HttpResponse object. It returned None instead." 51 % (middleware_method.__self__.__class__.__name__) 52 ) 53 54 try: 55 response = response.render() 56 except Exception as e: 57 response = self.process_exception_by_middleware(e, request) 58 59 return response 60 61 def process_exception_by_middleware(self, exception, request): 62 """ 63 Pass the exception to the exception middleware. If no middleware 64 return a response for this exception, raise it. 65 """ 66 for middleware_method in self._exception_middleware: 67 response = middleware_method(request, exception) 68 if response: 69 return response 70 raise

/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py

python

1import logging 2import sys 3from functools import wraps 4 5from django.conf import settings 6from django.core import signals 7from django.core.exceptions import ( 8 PermissionDenied, RequestDataTooBig, SuspiciousOperation, 9 TooManyFieldsSent, 10) 11... 12 13response.render() 14 15 return response 16 17 18def get_exception_response(request, resolver, status_code, exception, sender=None): 19 try: 20 callback, param_dict = resolver.resolve_error_handler(status_code) 21 response = callback(request, **dict(param_dict, exception=exception)) 22 except Exception: 23 signals.got_request_exception.send(sender=sender, request=request) 24 response = handle_uncaught_exception(request, resolver, sys.exc_info()) 25 26 return response 27 28 29... 30

user_auth/urls.py

python

1import django.contrib.auth.views 2from django.urls import path,include 3from . import views 4app_name='user_auth' 5 6urlpatterns=[ 7 path('top/',views.top_page, name="top"), 8 path('login/', 9 django.contrib.auth.views.LoginView, 10 { 11 'template_name': 'user_auth/login.html', 12 }, 13 name='login'), 14 path('logout/', 15 django.contrib.auth.views.LogoutView, 16 { 17 'template_name': 'user_auth/logout.html', 18 }, 19 name='logout'), 20]

試したこと

このエラーが起こる前にこのようなエラーが発生していて、

File "manage.py", line 22, in <module> execute_from_command_line(sys.argv) ... django.contrib.auth.views.login, AttributeError: module 'django.contrib.auth.views' has no attribute 'login'

こちらを参考にさせていただきdjango.contrib.auth.views.logindjango.contrib.auth.views.LoginViewに、django.contrib.auth.views.logoutdjango.contrib.auth.views.LogoutViewに変更させていただくとこのエラーは消えましたが、今回のエラーを解決できなかったので質問させていただいた次第です。初心者故至らないところもありますが何卒よろしくお願い申し上げます。

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

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

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

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

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

guest

回答2

0

私も詳しくないので不確かなのですが、

おそらく Djangoの汎用ビューを使われているので、
urls.pyのpathの部分に.as_view()が抜けているかと思います。

python

1urlpatterns=[ 2 path('top/',views.top_page, name="top"), 3 path('login/', 4 django.contrib.auth.views.LoginView.as_view(), #追加 5 { 6 'template_name': 'user_auth/login.html', 7 }, 8 name='login'), 9 path('logout/', 10 django.contrib.auth.views.LogoutView.as_view(), #追加 11 { 12 'template_name': 'user_auth/logout.html', 13 }, 14 name='logout'), 15]

これでどうでしょうか。

投稿2019/10/25 14:42

kota0525

総合スコア19

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

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

MorningMushroom

2020/02/10 14:53

私も同じエラーで悩んでいましたが、「.as_view()」を付けたら解決しました。ありがとうございます。
guest

0

お世話になります。本件全く同じ記事を参考にしていました。さらにエラーも全く同じ状況です。どのように解消されたかご教示頂けないでしょうか?

TypeError: init() takes 1 positional argument but 2 were given

クラスの指定に__init__がないというエラーなのは理解しているのですが、そもそもこちら側で設定したコードではない気がしています。

投稿2019/09/14 08:43

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0ae

2019/09/15 22:58

同じく僕も行き詰まって結局flaskに変更しました...参考にならず申し訳ございません。
退会済みユーザー

退会済みユーザー

2019/09/15 23:36

そうですかーー。全く同じエラーで全く同じ工程をすすんでいたのでとっても残念ですー
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問