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!

William Le
  • 825
  • 1
  • 9
  • 16

1 Answers1

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.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 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 – William Le Oct 23 '21 at 18:38
  • @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. – Willem Van Onsem Oct 23 '21 at 18:39
  • Thank you! Now I have password1 password2 required, I will try to solve this – William Le Oct 23 '21 at 18:42
  • @SeanWilliam: these are inherited from the "parent form", and since these are not rendered, thus do not appear as data in the POST request. – Willem Van Onsem Oct 23 '21 at 18:43