How do I customize a user registration form so it only requires email and password fields?

273 views
Skip to first unread message

Tom Tanner

unread,
Nov 19, 2017, 10:20:18 PM11/19/17
to Django users
I'm following this [tutorial](https://simpleisbetterthancomplex.com/tutorial/2017/02/18/how-to-create-user-sign-up-view.html) on making simple registration forms in Django. I'd like to make a user registration form that requires only two fields: "Email" and "Password." No second password field, just one.

So far, My `views.py` looks like this: 

    def register(request, template="register.html", redirect='/'):
    if request.method=="POST":
    form= RegisterForm(request.POST)
    if form.is_valid():
    form.save()
    email= form.cleaned_data.get("email")
    raw_password= form.cleaned_data.get("password1")
    user= authenticate(email=email, password=raw_password)
    login(request, user)
    return redirect('/')
    else:
    form= RegisterForm()
    return render(request, template, {"form": form})
 
`forms.py` has this class in it:

    class RegisterForm(UserCreationForm):
    email= forms.EmailField(label=_("Email"), max_length=254)
    
    class Meta:
    model= User
    fields= ("email",)

`register.html` looks simple:

    {% extends "base.html" %}    
    {% block main %}
    <h2>Register</h2>
    <form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Register</button>
    </form>
    {% endblock main %}

In `urls.py`, I have this line in `urlpatterns`: `url("^register/$", views.register, name="register"),`.

But my registration forms looks like this, with an Email field and two Password fields: http://i.imgur.com/b359A5Z.png. And if I fill out all three fields and hit "Register," I get this error: `UNIQUE constraint failed: auth_user.username`.

Any idea why I'm getting this error? And how can I make sure my form only has two fields: Email and Password?

Amitesh Sahay

unread,
Nov 19, 2017, 11:30:51 PM11/19/17
to dontsende...@gmail.com, Django users
Hello Tom, 

Django comes inbuilt with user authentication functions which is almost more than enough in most of the requirements.
In your case, you just need to import User model in models.py and in views.py under "if" statement just return HttpResponse. You dont need to write anything else. For email and password you don't need custom models and views. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/94d23f3b-25ee-437d-8302-ad80d6ecc1a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tom Tanner

unread,
Nov 20, 2017, 4:26:16 PM11/20/17
to Django users
Hello Amitesh,

When you say to return HttpReponse(), how would that code look? And I'm still learning Django, but how would this make it so that the only required fields on my registration form are "email" and "password?"

Thanks.

Tom Tanner

unread,
Nov 20, 2017, 5:39:34 PM11/20/17
to Django users
I changed a few things. 

`models.py` now has this:

    class MyUser(AbstractBaseUser):
    email= models.CharField(max_length=254, unique=True)
    USERNAME_FIELD= "email"
    
`forms.py`"

    class RegisterForm(UserCreationForm):
    email= forms.EmailField(label=_("Email"), max_length=254)
    
    class Meta:
    model= MyUser
    fields= ("email",)

`views.py`:

    def register(request, template="register.html", redirect='/'):
    if request.method=="POST":
    form= RegisterForm(request.POST)
    if form.is_valid():
    form.save()
    email= form.cleaned_data.get("email")
    raw_password= form.cleaned_data.get("password1")
    user= authenticate(username=email, password=raw_password)
    login(request, user)
    return redirect('/')
    else:
    form= RegisterForm()
    return render(request, template, {"form": form})

The form looks the same when rendered, with one "Email" field and two password fields. But now when I try to register I get this error: `'AnonymousUser' object has no attribute '_meta'`. And when navigated back to the form and reentered the email I used, the page refreshed and put this message above the form: `"My user with this Email already exists."`.

So Django makes the user, but there's a problem with processing stuff after the user's creation, I guess?

sunil mishra

unread,
Nov 21, 2017, 8:49:43 AM11/21/17
to Django users
Hi Tom ,
            i can solve your code problem . contact me on my mail :- sunilmi...@gmail.com
Reply all
Reply to author
Forward
0 new messages