我不明白这个错误是什么意思。看来我做的一切正确。

forms.py:

from django import forms

from django.forms import ModelForm

from .models import SignUpForm


class SignUpForm(forms.ModelForm):
    class Meta:
        model = SignUpForm
        fields = ['name', 'company', 'city', 'country', 'email', 'password']
        widgets = {
        'password': forms.PasswordInput(),
    }


这将抛出:


  AttributeError:'ModelFormOptions'对象没有属性'private_fields'


这是我的views.py

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
from .forms import signup
from django.utils.html import format_html
from django.contrib import messages
from .models import signup

def index(request):
    return render(request, 'myapp/index.html')

def signup(request):
    if request.method == 'POST':
        register_form = signup(request.POST)
        if register_form.is_valid():
            post = register_form.save()
            message = format_html("Hi {0}, Thank you for signing up with us! See your profile <a href=''>{1}</a>".format(register_form.cleaned_data['name'], "here"))
            form = signup()
            return render(request, 'myapp/register.html', {'signup':NewForm, 'message': message})
        else:
            message = register_form.errors
            form = signup()
            return render(request, 'myapp/register.html', {'signup':form, 'message': message})

    else:
        form = signup()

    return render(request, 'myapp/register.html', {'signup':form})

最佳答案

出于多种原因,拥有名为SignUpForm的模型是一个非常糟糕的主意。通常,以Form结尾的类应该是表单类,而不是模型类。更重要的是,这很可能是导致您出错的原因。

在您的views.py中,您可能正在执行以下操作:

from .models import SignUpForm
from .forms import SignUpForm

def some_view(request):
    if request.POST:
        form = SignUpForm(request.POST)
    else:
        form = SignUpForm(instance=SignUpForm(col1='foo', col2='bar'))


你猜怎么了?您只是将SignUpForm表单类的实例传递给了SignUpForm,而不是SignUpForm模型!

更改SignUpForm模型的名称,很可能不会出现错误。

如果这样做没有帮助,请显示此表单的views.py

10-06 14:24