From 37a5b578a45bec0476e94a73c5f9a3f9037a3b6d Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Sun, 2 Feb 2020 15:42:39 +0100 Subject: [PATCH] Create user and profile with all field needed --- apps/member/forms.py | 8 +++++++- apps/member/views.py | 18 +++++++++--------- templates/member/signup.html | 11 +++++++---- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/apps/member/forms.py b/apps/member/forms.py index 5dabef24..59d3fec2 100644 --- a/apps/member/forms.py +++ b/apps/member/forms.py @@ -16,9 +16,15 @@ from crispy_forms.bootstrap import InlineField, FormActions, StrictButton, Div, from crispy_forms.layout import Layout + +class SignUpForm(UserCreationForm): + class Meta: + model = User + fields = ['first_name','last_name','username','email'] + class ProfileForm(forms.ModelForm): """ - Forms pour la création d'un profile utilisateur. + A form for the extras field provided by the :model:`member.Profile` model. """ class Meta: model = Profile diff --git a/apps/member/views.py b/apps/member/views.py index 99e350bd..9048080e 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -16,7 +16,7 @@ from django_tables2.views import SingleTableView from .models import Profile, Club, Membership -from .forms import ProfileForm, ClubForm,MembershipForm, MemberFormSet,FormSetHelper +from .forms import SignUpForm, ProfileForm, ClubForm,MembershipForm, MemberFormSet,FormSetHelper from .tables import ClubTable,UserTable from .filters import UserFilter, UserFilterFormHelper @@ -29,10 +29,10 @@ class UserCreateView(CreateView): Une vue pour inscrire un utilisateur et lui créer un profile """ - form_class = ProfileForm + form_class = SignUpForm success_url = reverse_lazy('login') template_name ='member/signup.html' - second_form = UserCreationForm + second_form = ProfileForm def get_context_data(self,**kwargs): context = super().get_context_data(**kwargs) @@ -41,12 +41,12 @@ class UserCreateView(CreateView): return context def form_valid(self, form): - user_form = UserCreationForm(self.request.POST) - if user_form.is_valid(): - user = user_form.save() - user_profile = form.save(commit=False) # do not save to db - user_profile.user = user - user_profile.save() + profile_form = ProfileForm(self.request.POST) + if form.is_valid() and profile_form.is_valid(): + user = form.save() + profile = profile_form.save(commit=False) + profile.user = user + profile.save() return super().form_valid(form) class UserDetailView(LoginRequiredMixin,DetailView): diff --git a/templates/member/signup.html b/templates/member/signup.html index 67a85674..d95d86f6 100644 --- a/templates/member/signup.html +++ b/templates/member/signup.html @@ -1,14 +1,17 @@ {% extends 'base.html' %} {% load crispy_forms_tags %} +{% load i18n %} {% block title %}Sign Up{% endblock %} {% block content %}

Sign up

- {% csrf_token %} - {{ user_form|crispy }} - {{ form|crispy }} - + {% csrf_token %} + {{ form|crispy }} + {{ user_form|crispy }} +
{% endblock %}