__init __()只需1个参数(给定2个),出了什么问题?

时间:2014-11-20 04:49:30

标签: python django

它基本上是我正在工作的网站的注册表单,它也不起作用,但我现在的主要问题是__init__错误。你能检查出错的地方和地点吗?

views.py:

from django.views.generic.edit import FormView
from django.views.decorators.csrf import csrf_protect
from django.core.context_processors import csrf
from django.contrib import auth
from authactions import forms
from authactions.forms import CreateForm

class SignupView(FormView):
    template_name = 'authactions/register.html'
    form_class = forms.CreateForm

def form_valid(self, form):
    profile = form.saveUser()
    user = authenticate(username=profile.user.username ,password=profile.user.password)
    auth.login(self.request,user)
    return HttpResponseRedirect('/profiles/')

forms.py:

from django import forms
from profiles.models import UserProfile
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

class CreateForm(UserCreationForm):
    username = forms.CharField(required=True)
    password = forms.CharField(widget=forms.PasswordInput())
    password2 = forms.CharField(widget=forms.PasswordInput())
    email = forms.EmailField(required=True)

class Meta:
    model = UserProfile
    fields = ("username", "password", "password2", "email")

def saveUser(self):
    user = super(UserCreationForm, self).save()
    user.username = self.cleaned_data['username']
    user.password = self.cleaned_data['password']
    user.password2 = self.cleaned_data['password2']
    user.email = self.cleaned_data['email']
    if user.password != user.password2:
        raise forms.ValidationError("Mismatched password!")
    user.save()
    return user

class LoginForm(forms.Form):
    username = forms.CharField(required=True)
    password = forms.CharField(widget=forms.PasswordInput())

HTML:

<!DOCTYPE html>
<html>
<body>
        <div id = "signup_form">
            <form action="" method="POST">
            {% csrf_token %}
            {% if form.non_field_errors %}
                <ul>
                    {{ form.non_field_errors.as_ul }}
                </ul>
            {% endif %}
                <table>
                    <tr>
                        <td>
                            <h3>Personal Info</h3>
                        <input type = "text" name = "username" id = "username" class = "field" placeholder = "Username" required /><br />
                        <input type = "email" name = "email" id = "email" class = "field" placeholder = "Email" required />
                        <br />
                        <input type = "password" name = "password1" id = "password" class = "field" placeholder = "Password" required onkeyup = "check_length(1)"/><br />
                        <span>Password must be at least 7 characters long.</span><br />
                        <input type = "password" name = "password2" id = "password2" class = "field" placeholder = "Retype Password" required onkeyup = "check_password(1)" />
                    </td>
                </table>
                <input type = "submit" value = "Sign Up" id = "button" />
            </form>
        </div>
    </body>
</html>

错误详情(可能有用):

Request Method:     GET
Request URL:    http://127.0.0.1:8000/register/
Django Version:     1.6

Exception Type:     TypeError
Exception Value:    

__init__() takes exactly 1 argument (2 given)

Exception Location:     /usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response, line 114
Python Executable:  /usr/bin/python
Python Version:     2.7.6
Python Path:    

['/home/company5/Desktop/kenn-mark-x2/tunewhale2',
 '/usr/local/lib/python2.7/dist-packages/pip-1.5.6-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Sphinx-1.2.3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Jinja2-2.7.3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/docutils-0.12-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Pygments-1.6-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/MarkupSafe-0.23-py2.7-linux-x86_64.egg',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

回溯:

Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/register/

Django Version: 1.6
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'authactions',
'blog',
'profiles')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
          114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

Exception Type: TypeError at /register/
Exception Value: __init__() takes exactly 1 argument (2 given)

2 个答案:

答案 0 :(得分:3)

您没有显示您的urls.py,但问题可能在于:在映射基于类的视图时需要使用as_view:

url('^register/$', SignupView.as_view(), name='register')

答案 1 :(得分:0)

我无法判断它是复制粘贴还是代码实际上是这样,但class Meta必须在CreateForm类中缩进。实际上,saveUser方法也必须缩进。

相关问题