【问题标题】:Why Django registration-redux custom form field not showing up in the form?为什么 Django registration-redux 自定义表单字段没有显示在表单中?
【发布时间】:2017-03-16 19:43:04
【问题描述】:

我正在尝试在 Django 中构建一个自定义用户注册表单,它将做两件事 1)使用电子邮件字段作为用户名字段 2)添加一个额外的 company_name 字段以显示在表单中。我可以成功实现使用电子邮件作为用户名的第一个目标,但是当我执行 /accounts/register 时,第二个字段没有显示在表单中。

模型.py:

from django.db import models
from django.contrib.auth.models import BaseUserManager
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from phonenumber_field.modelfields import PhoneNumberField
from django_countries.fields import CountryField
from datetime import datetime    


class MyUserManager(BaseUserManager):

        def _create_user(self, email, password, company_name, **extra_fields):
            if not email:
                raise ValueError('The Email must be set')
            email = self.normalize_email(email)
            user = self.model(email=email, company_name=company_name, **extra_fields)
            user.set_password(password)
            user.save()
            return user

        def create_superuser(self, email, password, company_name, **extra_fields):
            extra_fields.setdefault('is_staff', True)
            extra_fields.setdefault('is_superuser', True)
            extra_fields.setdefault('is_active', 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(email, password, company_name, **extra_fields)

class User(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(unique=True, null=True)
    date_joined = models.DateTimeField(default=datetime.now)
    company_name = models.CharField(max_length=300, null=True)

    is_staff = models.BooleanField(
        _('staff status'),
        default=False,
        help_text=_('Designates whether the user can log into this site.'),
    )
    is_active = models.BooleanField(
        _('active'),
        default=True,
        help_text=_(
            'Designates whether this user should be treated as active. '
            'Unselect this instead of deleting accounts.'
        ),
    )
    USERNAME_FIELD = 'email'

    objects = MyUserManager()

    def __str__(self):
        return self.email

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.email

设置.py:

AUTH_USER_MODEL = 'usersignups.User'

如何将 company_name 带入this registration form

【问题讨论】:

    标签: python django forms


    【解决方案1】:

    在 models.py 中定义公司详细信息的新模型

    from django.contrib.auth.models import User
    # Create your models here.
    
    class company(models.Model):
       userkey = models.OneToOneField(User, primary_key=True)
       company_name = models.CharField(max_length=50)
    

    在forms.py中

    `from django.contrib.auth.models import User
     from registration.forms import RegistrationForm
     from .models import company
    
     class companyForm(RegistrationForm):
       company_name = forms.CharField(label='label here', max_length=50)
        class meta:
          model = company
          fields = ['company_name']`
    

    然后创建一个自定义视图来渲染compayForm并保存 希望它可以帮助你。对我来说效果很好

    【讨论】:

      猜你喜欢
      • 2016-07-06
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 2017-12-17
      • 2019-08-29
      • 1970-01-01
      相关资源
      最近更新 更多