2

Down here I'm trying to create a custom user registration form and this is the error I keep getting: enter image description here

That is the result of me using UserCreationForm in place of forms.ModelForm according to many sources.

I tried setting USERNAME_FIELD = username but it said

The field 'username' clashes with the field 'username' from model 'members.user' 

Before I switched to UserCreationForm the registration did not even create any new user. I successfully created the login functionality, but failed when it came to the registration. It did not show any response after registration form submission.

My code is as followed:

members/models.py

class User(models.Model): username = models.CharField(max_length=50, error_messages=login_errors_mess) password = models.CharField(max_length=50, error_messages=password_errors_mess) 

members/forms.py

class RegisterForm(UserCreationForm): class Meta: model = User fields = [ 'username', 'password', ] widgets = { 'username': forms.TextInput(attrs={ 'class': 'form-control', 'data-val': 'true', 'data-val-required': 'Please enter your user name'}), 'password': forms.TextInput(attrs={ 'class': 'form-control', 'data-val': 'true', 'data-val-required': 'Please enter your password' }), } 

members/views.py

def register(request): form = RegisterForm() if request.method == "POST": form = RegisterForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('login') # return redirect('login') form = RegisterForm() return render (request, "registration/register.html", context={"form": form}) 

templates/registration/register.html

<form method="POST" action="/register/"> {% csrf_token %} {% comment %} {{ form }} {% endcomment %} <div class="form-group"> <label for="username">Username</label> <input type="text" class="form-control" name="username" id="username"/> </div> <div class="form-group"> <label for="password">Password</label> <input type="password" class="form-control" name="password" id="password"/> </div> <button type="submit" class="btn btn-primary btn-flat m-b-30 m-t-30">Register</button> </form> 

urls.py

urlpatterns = [ path('members/', include('members.urls')), path('admin/', admin.site.urls), path('', dashboard, name='dashboard'), path('register/', register, name='register'), path('login/', login_page, name='login'), ] 

Could you show me how to solve this? Thank you!

1 Answer 1

3

You should specify the name of the field as a string, so:

class User(models.Model): username = models.CharField(max_length=50, error_messages=login_errors_mess) password = models.CharField(max_length=50, error_messages=password_errors_mess) # not USERNAME_FIELD = username USERNAME_FIELD = 'username'

This is however not sufficient: a user model should implement extra logic, for example to check permissions.

The Django documentation has a section named using a custom user model when starting a project which explains how to set up a custom user model.

Sign up to request clarification or add additional context in comments.

4 Comments

It fixed the username_field error. But unfortunately I return to the original error: after submitting the form nothing happens, I stay on the same registration page
@SeanWilliam: please remove the bottom form = RegisterForm() line (in the register view), this will remove the errors the form is trying to report in the POST branch.
Thank you! Now I have password1 password2 required, I will try to solve this
@SeanWilliam: these are inherited from the "parent form", and since these are not rendered, thus do not appear as data in the POST request.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.