自定义用户的显示表单

时间:2019-07-04 20:51:50

标签: python django

我将按照以下教程在Django中构建自定义用户: https://wsvincent.com/django-custom-user-model-tutorial/

我设法创建它并添加新字段,例如:电话,位置,名字等。

但是,当我单击signup时,这些新字段不会出现。

enter image description here

我确实知道如何做:

1)出现添加的字段

2)用该特定html注册页面的引导程序修改html / css,因为我找不到它

我尝试在admin.py的{​​{1}}中添加这些字段,但不能正常工作

list_display = ['email', 'username']中,我有:

admin.py

from django.contrib import admin from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin from .forms import CustomUserCreationForm, CustomUserChangeForm from .models import CustomUser class CustomUserAdmin(UserAdmin): add_form = CustomUserCreationForm form = CustomUserChangeForm model = CustomUser list_display = ['email', 'username'] # HERE I TRIED TO ADD THE FIELDS SUCH AS 'location' BUT IT DOES NOT WORK admin.site.register(CustomUser, CustomUserAdmin) 中,我有:

forms.py

在我的from django import forms from django.contrib.auth.forms import UserCreationForm, UserChangeForm from .models import CustomUser class CustomUserCreationForm(UserCreationForm): class Meta(UserCreationForm): model = CustomUser fields = ('username', 'email') class CustomUserChangeForm(UserChangeForm): class Meta(UserChangeForm): model = CustomUser fields = ('username', 'email', 'first_name', 'last_name', 'organization', 'location', 'postcode', 'phone') 中,我有:

models.py

`from django.contrib.auth.models import AbstractUser from django.db import models class CustomUser(AbstractUser): first_name = models.CharField(max_length=100, default='SOME STRING') last_name = models.CharField(max_length=100, default='SOME STRING') organization = models.CharField(max_length=100, default='SOME STRING') location = models.CharField(max_length=100, default='SOME STRING') postcode = models.CharField(max_length=100, default='SOME STRING') phone = models.CharField(max_length=100, default='SOME STRING') def __str__(self): return self.email ` 中,我有:

views.py

from django.urls import reverse_lazy from django.views.generic.edit import CreateView from .forms import CustomUserCreationForm class SignUpView(CreateView): form_class = CustomUserCreationForm success_url = reverse_lazy('login') template_name = 'signup.html' 中,我有:

signup.html

1 个答案:

答案 0 :(得分:1)

新字段未显示在注册表单上,因为尚未将它们添加到CustomUserCreationForm的fields属性中。

class CustomUserCreationForm(UserCreationForm):
    class Meta(UserCreationForm):
        model = CustomUser
        fields = ('username', 'email')

应该是:

class CustomUserCreationForm(UserCreationForm):
    class Meta(UserCreationForm):
        model = CustomUser
        fields = ('username', 'email', 'first_name', 'last_name','organization', 'location', 'postcode', 'phone')