User creation form returns back to the same register page with no errors

Hi, so i am trying to make an register page by using some custom forms and templates.

now i want my from to be returned to the homepage which it does not for some reason and also want to hide the password in the register page when user types it in, how can i do that :(.

I have done this before using crispy forms but i am not using them at the moment, could it be the custom template is the problem ?

My forms File: from django import formsfrom django.contrib.auth.models import Userfrom -

My views File: our views here.def register(request): if request.method == 'POST': -

My Templates File: {% extends 'blog/base.html' %}{% block content %}<div class="container"> -

My Register Page ( Want to hide the passwords when user types it in):

Thank you :slight_smile:

Couple different things here, and I’m not sure this list is complete:

  1. In your template you have
    <input type='{{form.username}}' class="form-control" placeholder="Enter Username">
    This is not how you render form fields. See Working with forms - there’s a lot of information on that page about creating templates and the ways you can render forms in your template.

  2. In your form you have:

		model = User
		fields = '__all__'

Using all in this situation isn’t a good idea. Are you aware of all the fields in the User model? Do you want them all rendered and entered in your form? (On top of that, you’re not even properly rendering the form, so the fields you’re identifying as being in the form aren’t present.)

  1. I’m guessing you’re not returning to your home page because the form being submitted is failing the is_valid test. (See the two points above). This would become more “visible” to you if you handled the failing form case with rendering the error messages.

  2. The password field is visible because you’re not rendering the password form field correctly. (See #1 above)

If you’re looking for a good example for how to handle those forms, I’d suggest reading the source for the forms, the views and the templates supplied with Django in django.contrib.auth.

1 Like

For future reference, please post your code / templates / etc here, rather than referencing external sites. You can post your code between lines consisting of three backtick - ` characters. This means you’ll have one line of ```, then your code, then another line of ```. This will allow the forum software to keep your code formatted properly.

1 Like

hi sorry for the late response, as my topic was getting approved.
so i have edited the views,forms and template file but it still does not redirect to home page, can you take a look please :slight_smile: :grinning:

My Forms File:

from django import forms

from django.contrib.auth.models import User

from django.contrib.auth.forms import UserCreationForm

class UserCreateForm(UserCreationForm):
	email = forms.EmailField()

	class meta:
		model = User
		fields = ['email','password1','password2']

My Views File:

from django.shortcuts import render, redirect

from .forms import UserCreateForm

from django.contrib import messages

# Create your views here.

def register(request):

	if request.method == 'POST':

		form = UserCreateForm(request.POST)

		if form.is_valid():
			email = form.cleaned_data.get('email')
			messages.success(request,f"account created for {email}")
			return redirect ('home')



		form = UserCreateForm()

	context = {'form': form,
			   'title': 'Register Page'}

	return render(request, 'users/register.html', context)

My Templates File:

{% extends 'blog/base.html' %}

{% load crispy_forms_tags %}

{% block content %}

<div class="container">

  <h2>Please Register Here </h2>

<form method="post">

  {% csrf_token %}

<div class="form-group">
  {{|as_crispy_field }}

<div class="form-group">
  {{ form.password1|as_crispy_field }}

<div class="form-group">
  {{ form.password2|as_crispy_field }}

<button class="btn btn-outline-info" type="submit">Sign Up</button>


{% endblock content %}

EDIT 2: SO i Finally figured it out:

ok finally i figured it out.

class meta, i had to put class Meta (capital M)

and also fields = [‘email’,‘password1’,‘password2’]

i can register users now by email :slight_smile:

1 Like