Home > other >  Django - ModelForm has no model class specified
Django - ModelForm has no model class specified

Time:05-11

Django is giving me the following error:

ModelForm has no model class specified

Traceback

Traceback (most recent call last):
  File "C:\Users\Laila\.virtualenvs\BlogProject-71CaIFug\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Users\Laila\.virtualenvs\BlogProject-71CaIFug\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Laila\.virtualenvs\BlogProject-71CaIFug\lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "D:\Programming\Python\Projects\Django\BlogProject\django_project\users\views.py", line 35, in profile
    userUpdateForm = UserUpdateForm()
  File "C:\Users\Laila\.virtualenvs\BlogProject-71CaIFug\lib\site-packages\django\forms\models.py", line 356, in __init__
    raise ValueError("ModelForm has no model class specified.")

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

Here's my code:

forms.py

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


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

    # A class to specify the model the form is going to interact with
    class Meta:
        model = User

        # Fields wanted in the form and in what order:
        fields = ['username', 'email', 'password1', 'password2']


# A model form is a form that allows the creation of a form that will work with a specific database model
class UserUpdateForm(forms.ModelForm):
    model = User
    fields = ['username', 'email']


class ProfileUpdateForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ['image']

views.py

from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm
from django.contrib.auth.decorators import login_required



def register(request):
    # If it gets a POST request then it instantiates a user creation form with that POST data
    if request.method == 'POST':
        # Create a form that has the request POST data:
        form = UserRegisterForm(request.POST)

        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, 'Your account was created. You are now able to log in')
            return redirect('login')
    # With any other request it creates an empty user creation form
    else:
        form = UserRegisterForm()
    return render(request, 'users/register.html', {'form': form})


@login_required
def profile(request):
    userUpdateForm = UserUpdateForm()
    profileUpdateForm = ProfileUpdateForm()

    context = {
        'userUpdateForm': userUpdateForm,
        'profileUpdateForm': profileUpdateForm
    }
    return render(request, 'users/profile.html', context)

I've spent a couple of hours searching for similar questions here on stackoverflow but most solutions point to typos. I've thoroughly checked my code and I'm fairly certain I've spelled everything correctly and according to convention.

What am I doing wrong? Any help is appreciated. Thank you.

CodePudding user response:

Can you try this? For more about Meta class, check here.

class UserUpdateForm(forms.ModelForm):
  class Meta:
    model = User
    fields = ['username', 'email']
  • Related