Django allauthを使った認証機能実装をしようとしています。
formに入力して送信を押してもテーブルに反映されません。
form.py
form.py
1from django.utils.translation import gettext, gettext_lazy as _, pgettext 2from allauth.account.forms import SignupForm 3from django import forms 4from .models import CustomUser, Administrater 5from allauth.account.adapter import DefaultAccountAdapter 6 7class CustomSignupForm(SignupForm): 8 username = forms.CharField( 9 label=_("会社名"), 10 help_text='※必須', 11 widget=forms.TextInput( 12 attrs={"placeholder": _("会社名"), "autocomplete": "username", 'class':'companyinformation'} 13 ), 14 ) 15 company_name_kana = forms.CharField( 16 label=("会社名カナ"), 17 help_text='※必須', 18 widget=forms.TextInput( 19 attrs={"placeholder":"会社名カナ", 'class':''} ) ) 20 電話番号 = forms.CharField( 21 help_text='任意', 22 widget=forms.TextInput( 23 attrs={'placeholder':'電話番号', 'class':''})) 24 email = forms.EmailField( 25 help_text='※必須', 26 widget=forms.TextInput( 27 attrs={ 28 "type": "email", 29 "placeholder": _("E-mail address"), 30 "autocomplete": "email", 31 } 32 ) 33 ) 34 representative = forms.CharField( 35 label=('代表者名'), 36 help_text='※必須', 37 widget=forms.TextInput( 38 attrs={'placeholder':'代表者名', 'class': ''})) 39 40 # 担当者情報 41 administrater_lastname = forms.CharField( 42 label=('担当者名 性'), 43 help_text='※必須', 44 widget=forms.TextInput( 45 attrs={'placeholder':'担当者名 性', 'class': ''})) 46 47 class Meta: 48 model = CustomUser 49 50 def signup(self, request,user): 51 user.phone_number = self.cleaned_data['電話番号'] 52 user.company_name = self.cleaned_data['username'] 53 user.company_name_kana = self.cleaned_data['company_name_kana'] 54 user.representative = self.cleaned_data['representative'] 55 user.administrater_lastname = self.cleaned_data['administrater_lastname'] 56 user.save() 57 return user
models.py
models.py
1from django.db import models 2from django.contrib.auth.models import AbstractUser 3 4class CustomUser(AbstractUser): 5 class Meta: 6 verbose_name_plural = '会社情報' 7 8 phone_number = models.CharField('電話番号', max_length=11,blank=True, default=0) 9 company_name = models.CharField('会社名', max_length=500,blank=True, default=0) 10 company_name_kana = models.CharField('会社名カナ', max_length=500, blank=True, default=0) 11 representative = models.CharField('代表者名', max_length=500, blank=True, default=0) 12 13 14# Create your models here. 15 16class Administrater(models.Model): 17 """docstring for Administrater""" 18 def __init__(self, arg): 19 super(Administrater, self).__init__() 20 self.arg = arg 21 22 def __str__(self): 23 return self.name 24 25 class Meta: 26 verbose_name_plural = "管理者情報" 27 28 CustomUser = models.ForeignKey(CustomUser, on_delete=models.CASCADE ,related_name='costomuser') 29 administrater_lastname = models.CharField('管理者情報', max_length=500,blank=True, default=0)
signup.html
signup.html
1{% load i18n %} 2{% load static %} 3<!DOCTYPE html> 4<html> 5<head> 6 <link rel="stylesheet" href="{% static 'css/accounts.css' %}"> 7</head> 8<body> 9{% block content %} 10<div class="container"> 11 <div class="row"> 12 <div class="my-div-style"> 13 <h1>会員登録</h1> 14 <h2>企業情報</h2> 15 <form action="{% url 'account_signup' %}" method="post" > 16 {% csrf_token%} 17 {{form.as_p}} 18 19 {% if redirect_field_value %} 20 <input type="hidden" name="{{ redirect_field_name}}" value="{{ redirect_field_value}}" /> 21 {% endif %} 22 <button class="btn btn-primary" type="submit">次へ</button> 23 </form> 24 </div> 25 </div> 26</div> 27{% endblock %} 28</body> 29</html> 30
adapter.py
from allauth.account.adapter import DefaultAccountAdapter class AccountAdapter(DefaultAccountAdapter): def save_user(self, request, user, form, commit=True): """ This is called when saving user via allauth registration. We override this to set additional data on user object. """ # Do not persist the user yet so we pass commit=False # (last argument) user = super(AccountAdapter, self).save_user(request, user, form, commit=False) user.phone_number = form.cleaned_data.get('電話番号') user.company_name = form.cleaned_data.get('username') user.company_name_kana =form.cleaned_data.get('company_name_kana') user.representative = form.cleaned_data.get('representative') user.administrater_lastname = form.cleaned_data.get('administrater_lastname') user.save()
フィールドに入力し、登録ボタンを押すときちんと遷移し、customuserテーブルにはデータが入りますが、administraterテーブルには入りません。
なぜでしょうか?
あなたの回答
tips
プレビュー