Django - ModelForm 没有指定模型 class

Django - ModelForm has no model class specified

Django 给我以下错误:

ModelForm has no model class specified

回溯

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.

这是我的代码:

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)

我花了几个小时在 Whosebug 上搜索类似问题,但大多数解决方案都指向拼写错误。我已经彻底检查了我的代码,我相当确定我已经按照惯例正确拼写了所有内容。

我做错了什么?任何帮助表示赞赏。谢谢。

你能试试这个吗?有关 Meta class 的更多信息,请查看 here

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