Reputation: 15
I'm making a social network in django and i've a trouble with the User Creation Form. Here's my code:
def register(request):
if request.method == 'POST':
form = UserProfileForm(request.POST) #
if form.is_valid():
u = UserProfile.objects.create(first_name = form.cleaned_data['first_name'],...)
u.set_password(u.password)
u.save()
return HttpResponseRedirect(...)
else:
return Http404
else:
form = UserProfileForm()
return render_to_response(...,
{'form': form},
context_instance=RequestContext(request))
class UserProfileForm(ModelForm):
first_name = forms.CharField(help_text="Por favor, introduzca su nombre", required=True)
...
class Meta:
model = UserProfile
fields = ['first_name',...]
def __init__(self, request=None, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(UserProfileForm, self).__init__(*args, **kwargs)
def validate_user(self):
first_name_length = len(self.cleaned_data['first_name'])
...
if not first_name_length > 0:
raise ValidationError("Por favor, introduzca nombre valido")
...
class UserProfile(models.Model):
first_name = models.CharField(max_length=50, blank=True)...
and i'm using templates with form.as_p, but my form instance is never valid. What am I doing wrong? Can anyone help me?
Upvotes: 0
Views: 90
Reputation: 600041
Look at the signature of your form:
def __init__(self, request=None, *args, **kwargs)
and now look at how you instantiate it:
form = UserProfileForm(request.POST)
So, you're passing request.POST
as the request parameter.
You should really avoid changing the signature of a subclass like this. In particular in your case as you are not using or passing the request at all. If you do need to do this, you should do the same as with the user
value which you get from kwargs
(but again you're not using or passing that either, so I don't know why you're bothering to do it.)
Upvotes: 1