Reputation: 108
It's basically a signup form for a site I'm working and its also not working too, but my main problem right now is the __init__
error. Can you check what and where it went wrong?
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>
error details (might be useful):
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']
traceback:
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)
Upvotes: 0
Views: 1722
Reputation: 599450
You don't show your urls.py, but the problem is probably there: you need to use as_view when mapping a class based view:
url('^register/$', SignupView.as_view(), name='register')
Upvotes: 3
Reputation: 1822
I can't tell if it is the copy-paste or whether your code actually looks like that, but the class Meta
has to be indented within your CreateForm
class. In fact, the saveUser
method has to be indented as well.
Upvotes: 0