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

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

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

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

Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Python

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

Q&A

0回答

1757閲覧

Django AbstractBaseUserでのカスタムユーザの必要設定 (AssertionErrorが発生します。)

Madai

総合スコア29

Django

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

Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Python

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

0グッド

0クリップ

投稿2020/09/24 10:13

編集2020/09/30 07:16

Djangoにてカスタムユーザーを定義し、
ログイン認証はallauthを利用しておりますが、実行時(アクセス時)にAssertionErrorが発生します。
(現状、個人的にテストクラスを用意していない。 エラー詳細がNo exception message suppliedと表示されているため、ここで発せられるAssertionErrorは もともとDjangoに組み込まれていた内容に未設定があるため、発せられてるものと推測します。)
settings.pyにカスタムユーザの仕様を設定する必要がありそうなのですが、現状設定しているものからの不足分が調べてもわかりかねました。(もしくはカスタムユーザーのオーバーライドに不足がある?)
問題解決へお力添え頂きたく、どうぞよろしくお願いします。

【エラーメッセージ】 AssertionError at /accounts/login/ No exception message supplied Request Method: GET Request URL: http://localhost:8000/accounts/login/ Django Version: 3.1 Exception Type: AssertionError Exception Location: C:\Users\husah\paperwork_system\lib\site-packages\allauth\account\forms.py, line 127, in __init__ Python Executable: C:\Users\husah\paperwork_system\Scripts\python.exe Python Version: 3.8.2 Python Path: ['C:\Users\husah\Dropbox\見積システム\paperwork_system', 'C:\Users\husah\AppData\Local\Programs\Python\Python38\python38.zip', 'C:\Users\husah\AppData\Local\Programs\Python\Python38\DLLs', 'C:\Users\husah\AppData\Local\Programs\Python\Python38\lib', 'C:\Users\husah\AppData\Local\Programs\Python\Python38', 'C:\Users\husah\paperwork_system', 'C:\Users\husah\paperwork_system\lib\site-packages'] Server time: Wed, 30 Sep 2020 15:37:57 +0900 Traceback Switch to copy-and-paste view C:\Users\husah\paperwork_system\lib\site-packages\django\core\handlers\exception.py, line 47, in inner response = get_response(request) … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\django\core\handlers\base.py, line 179, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\django\views\generic\base.py, line 73, in view return self.dispatch(request, *args, **kwargs) … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\django\utils\decorators.py, line 43, in _wrapper return bound_method(*args, **kwargs) … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\django\views\decorators\debug.py, line 89, in sensitive_post_parameters_wrapper return view(request, *args, **kwargs) … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\allauth\account\views.py, line 138, in dispatch return super(LoginView, self).dispatch(request, *args, **kwargs) … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\allauth\account\views.py, line 78, in dispatch response = super(RedirectAuthenticatedUserMixin, … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\django\views\generic\base.py, line 101, in dispatch return handler(request, *args, **kwargs) … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\allauth\account\views.py, line 94, in get response = super(AjaxCapableProcessFormViewMixin, self).get( … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\django\views\generic\edit.py, line 133, in get return self.render_to_response(self.get_context_data()) … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\allauth\account\views.py, line 163, in get_context_data ret = super(LoginView, self).get_context_data(**kwargs) … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\django\views\generic\edit.py, line 66, in get_context_data kwargs['form'] = self.get_form() … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\allauth\account\views.py, line 113, in get_form form = super(AjaxCapableProcessFormViewMixin, self).get_form( … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\django\views\generic\edit.py, line 33, in get_form return form_class(**self.get_form_kwargs()) … ▶ Local vars C:\Users\husah\paperwork_system\lib\site-packages\allauth\account\forms.py, line 127, in __init__ assert app_settings.AUTHENTICATION_METHOD \ … ▶ Local vars

実際のソースコード(modelsとsettings)は以下のとおりです。

【accounts\models.py】 from django.db import models from django.contrib.auth.base_user import BaseUserManager from django.contrib.auth.base_user import AbstractBaseUser from django.contrib.auth.models import PermissionsMixin from django.contrib.auth.validators import UnicodeUsernameValidator from django.utils.translation import gettext_lazy as _ from django.utils import timezone class Users_manager(BaseUserManager): use_in_migrations = True def _create_user(self, user_id, password, name,**extra_fields): if not user_id: raise ValueError('The given user_id must be set') user_id = self.model.normalize_user_id(user_id) user = self.model(user_id=user_id, name=name, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_user(self, user_id, password=None, name=None, **extra_fields): extra_fields.setdefault('is_active', False) extra_fields.setdefault('is_superuser', False) return self._create_user(user_id, password, name, **extra_fields) def create_superuser(self, user_id, password=None, name=None, **extra_fields): # extra_fields.setdefault('is_staff', True) extra_fields.setdefault('is_superuser', True) # if extra_fields.get('is_staff') is not True: # raise ValueError('Superuser must have is_staff=True.') if extra_fields.get('is_superuser') is not True: raise ValueError('Superuser must have is_superuser=True.') return self._create_user(user_id, password, name, **extra_fields) def with_perm(self, perm, is_active=True, include_superusers=True, backend=None, obj=None): if backend is None: backends = auth._get_backends(return_tuples=True) if len(backends) == 1: backend, _ = backends[0] else: raise ValueError( 'You have multiple authentication backends configured and ' 'therefore must provide the `backend` argument.' ) elif not isinstance(backend, str): raise TypeError( 'backend must be a dotted import path string (got %r).' % backend ) else: backend = auth.load_backend(backend) if hasattr(backend, 'with_perm'): return backend.with_perm( perm, is_active=is_active, include_superusers=include_superusers, obj=obj, ) return self.none() class Users(AbstractBaseUser, PermissionsMixin): userid_validator = UnicodeUsernameValidator() user_id = models.CharField( primary_key=True, max_length=255, validators=[userid_validator], error_messages={ 'unique': _("A user with that user_id already exists."),# 要確認 from django.utils.translation import gettext_lazy as _ }, ) name = models.CharField(max_length=255) is_active = models.BooleanField( _('active'), default=True, help_text=_( 'Designates whether this user should be treated as active. ' 'Unselect this instead of deleting accounts.' ), ) date_joined = models.DateTimeField(_('date joined'), default=timezone.now) objects = Users_manager() USERNAME_FIELD = 'user_id' REQUIRED_FIELDS = ['name']# ['user_id','name'] REQUIRED_FIELDSは必須を定義しているが、USERNAME_FIELDで定義してある場合は不要っぽい? class meta: verbose_name_plural = 'Users' def get_name(self): return self.name
【settings.py】 #略 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'paperwork_system', 'top', 'client', 'quotation', 'accounts.apps.AccountsConfig', 'django.contrib.sites', 'allauth', 'allauth.account', 'allauth.socialaccount',#allauth\templates\account\login.html(デフォルトのlogin)を利用する際に必要 ] ########略 以下カスタムユーザーにかかわる設定############# AUTH_USER_MODEL = 'accounts.Users' #django-allauthで利用するdjango.contrib.sitesを使うためのサイト識別用ID SITE_ID = 1 AUTHENTICATION_BACKENDS = ( 'allauth.account.auth_backends.AuthenticationBackend', 'django.contrib.auth.backends.ModelBackend', )#不要の可能性あり? ACCOUNT_AUTHENTICATION_METHOD = 'user_id' ACCOUNT_USERNAME_REQUIRED = False ACCOUNT_EMAIL_REQUIRED = False ACCOUNT_USER_MODEL_USERNAME_FIELD = None LOGIN_REDIRECT_URL = '/top' ACCOUNT_LOGOUT_REDIRECT_URL = 'accounts/login' ACCOUNT_LOGOUT_ON_GET = True MEDIA_ROOT = os.path.join(BASE_DIR,'media') MEDIA_URL = '/media/'

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問