joshlsullivan
joshlsullivan

Reputation: 1500

Django UserCreationForm Error: ModelForm has no model class specified

I'm trying to create a page for is_staff folks can create users. The code is below.

views.py

@login_required
def create_new_user(request):
    if request.method == 'POST':
        form = CreateUserForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('jobs:new-user'))
    else:
        form = CreateUserForm()
        return render(request, 'jobs/create_user.html', {'form': form})

forms.py

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

class CreateUserForm(UserCreationForm):
    class Meta:
        model: User
        fields = ['username', 'password1', 'password2']

error output

This is the error I get when I try to access the account creation page.

Traceback:

File "/Users/joshsullivan/github/sm8_portal/env/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Users/joshsullivan/github/sm8_portal/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Users/joshsullivan/github/sm8_portal/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/joshsullivan/github/sm8_portal/env/lib/python3.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/Users/joshsullivan/github/sm8_portal/env/lib/python3.6/site-packages/jobs/views.py" in create_new_user
  124.         form = CreateUserForm()

File "/Users/joshsullivan/github/sm8_portal/env/lib/python3.6/site-packages/django/contrib/auth/forms.py" in __init__
  96.         super(UserCreationForm, self).__init__(*args, **kwargs)

File "/Users/joshsullivan/github/sm8_portal/env/lib/python3.6/site-packages/django/forms/models.py" in __init__
  275.             raise ValueError('ModelForm has no model class specified.')

Exception Type: ValueError at /create_user/
Exception Value: ModelForm has no model class specified.

Any ideas? As always, THANK YOU.

Upvotes: 0

Views: 863

Answers (1)

Michael Bruce
Michael Bruce

Reputation: 11377

You need to set model equal to User like model = User

class CreateUserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username', 'password1', 'password2']

Upvotes: 2

Related Questions