elmo330
elmo330

Reputation: 383

How do I extend UserCreationForm to include email field

I managed to get the stock standard User Creation Form to work. Which included just the username, password1 and password2 field. However, when I try to include the email field it never shows up in my template. I think I'm missing something in my view perhaps. Here is my code:

forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

class UserCreationForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2")

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()
        return user

views.py

from django.contrib.auth.forms import UserCreationForm 

def register_user(request):
if request.method == 'POST':
    form = UserCreationForm(request.POST)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/')

args = {}
args.update(csrf(request))

args['form'] = UserCreationForm()

return render_to_response('stories/register.html', args)

register.html

<form action = "/register/" method = "POST"> 
    {% csrf_token %} 

    <p>
    {{ form.username.label_tag }}
    {{ form.username}}
    </p>

    <p> 
    {{ form.email.label_tag }}
    {{ form.email }}
    </p>

    <p>
    {{ form.password1.label_tag }}
    {{ form.password1 }}
    </p>

    <p>
    {{ form.password2.label_tag }}
    {{ form.password2 }}
    </p>

    <input type = "submit" value = "register" />
</form>

All of the fields in this file are being rendered into the view, Except the email field.

Can anyone spot why?!

Upvotes: 14

Views: 12890

Answers (4)

Hesam Marshal
Hesam Marshal

Reputation: 319

  1. In forms.py change the name of class UserCreationForm from to something else:

UserCreationForm -> ProfileCreation Form

class ProfileCreation(UserCreationForm)

  1. in views.py import this class:

from . forms import ProfileCreation

You have imported the UserCreationForm from django.contrib.auth.forms in your view. So the forms.py has been skipped.

Upvotes: 0

Điệp
Điệp

Reputation: 65

forms.py

from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

class SignupForm(UserCreationForm):
    class Meta:
        model = User
        fields = ("username", "email",)

views.py

from django.urls import reverse_lazy
from django.views import generic
from accounts.forms import SignupForm

class SignUpView(generic.CreateView):
    form_class = SignupForm
    success_url = reverse_lazy('login')
    template_name = 'stories/register.html'

Upvotes: 0

Ornella Ramos
Ornella Ramos

Reputation: 51

I'm new with django and I tried what you posted and I had to changed to work ... Here's what I did.

forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User


class UserCreationForm(UserCreationForm):
    email = forms.EmailField(required=True, label='Email')

    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2")

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()
        return user

views.py

from .forms import UserCreationForm
from django.urls import reverse_lazy
from django.views import generic


class SignUp(generic.CreateView):
    form_class = UserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'accounts/signup.html'

signup.html

{% extends 'polls/base.html' %}
{% load bootstrap4 %}
{% load static %}
{% block content %}
<body class="body_login">
  <form method="post" class="form-signup">
    {% csrf_token %}
    {% bootstrap_form form  %}
    <button type="submit" class="save btn btn-dark">Sign up</button>
  </form>
</body>
{% endblock %}

Upvotes: 5

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52203

You are importing the wrong UserCreationForm in views.py. You should import your own form not the Django's one:

stories/views.py

from stories.forms import UserCreationForm
...

Besides that, you don't have to wrap all your fields with <p></p> individually as there exists form.as_p() for this job.

register.html

<form action = "/register/" method = "POST">{% csrf_token %}
    {{ form.as_p }}
</form>

Hope this helps.

Upvotes: 7

Related Questions