Django 表单错误(__init__() 采用 1 个位置参数,但给出了 2 个)

Django form errors (__init__() takes 1 positional argument but 2 were given)

我使用 Django 1.11 和 python 3.6.5。我制作了一个模型和一个用户注册表格。一切似乎都很好,除了我有这个我无法解决的错误。

__init__() takes 1 positional argument but 2 were given

这是模型:

from django.db import models

class User_account(models.Model):

first_name = models.CharField(max_length=64)
last_name = models.CharField(max_length=64)
user_name = models.CharField(max_length=128)
email = models.EmailField(max_length=254, blank=False, unique=True)
password = models.CharField(max_length=64) 
address = models.ForeignKey(Address, on_delete=models.CASCADE)

表格如下:

from django import forms
from .models import User_account

class User_accountForm(forms.ModelForm):

    class Meta:
        model = User_account
        fields = ['first_name', 'last_name', 'user_name', 'email', 'password', 'address']

这是部分视图:

from django.shortcuts import render, redirect
from .models import *
from django.views import View
from .forms import *
from django.contrib.auth import authenticate, login


class User_accountFormView(View):
    form_class = User_accountForm
    template_name = 'towns/salehslist/registeration_form.html'

    # display a blank form
    def get(self, request, *args, **kwargs):
        form = self.form_class(None)

        return render(request, self.template_name, {'form':form})

网址:

from django.conf.urls import url

from . import views

urlpatterns = [

    url(r'^$', views.index, name='index'),
    url(r'^register/$', views.User_accountFormView, name='register'),

当您在 url 模式中包含基于 class 的视图时,您需要调用 as_view()

url(r'^register/$', views.User_accountFormView.as_view(), name='register'),