Prova12
Prova12

Reputation: 653

Display form for a Custom User

I am build a custom user in Django by following this tutorial: https://wsvincent.com/django-custom-user-model-tutorial/

I manage to create it and add new fields such as: phone, location, firstname etc.

However, when I click signup, these new fields do not appear.

enter image description here

I do know know how to:

1) Make the added fields appear

2) Modify the html/css with bootstrap of that specific html signup page because I cannot find it

I tried to add these fields in admin.py in list_display = ['email', 'username'] but doe snot work

In admin.py I have:

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)

In forms.py I have:

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')

In my models.py I have:

`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
`

In views.py I have:

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'

In signup.html I have:

{% extends 'base.html' %}

{% block title %}Sign Up{% endblock %}

{% block content %}
<h2>Sign up</h2>
<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Sign up</button>
</form>
{% endblock %}

Upvotes: 0

Views: 48

Answers (1)

axevalley
axevalley

Reputation: 335

The new fields are not showing on your sign up form because you haven't added them to the fields attribute of CustomUserCreationForm.

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

Should be:

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

Upvotes: 1

Related Questions