Skip to content

Instantly share code, notes, and snippets.

@macndesign
Created October 23, 2012 18:04
Show Gist options
  • Save macndesign/3940394 to your computer and use it in GitHub Desktop.
Save macndesign/3940394 to your computer and use it in GitHub Desktop.
registro de usuario e userprofile
# forms.py
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
exclude = ('user',)
class UserForm(UserCreationForm):
def __init__(self, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email')
# views.py
def register(request):
userform = UserForm(request.POST or None)
userprofileform = UserProfileForm(request.POST or None)
if userform.is_valid() and userprofileform.is_valid():
user = userform.save()
userprofile = userprofileform.save(commit=False)
userprofile.user = user
userprofile.save()
return redirect(reverse_lazy('accounts:register'))
return render_to_response(
'accounts/register.html',
dict(userform=userform, userprofileform=userprofileform),
context_instance=RequestContext(request),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment