Create user and profile with all field needed

This commit is contained in:
Pierre-antoine Comby 2020-02-02 15:42:39 +01:00
parent c4a5970ca3
commit 37a5b578a4
3 changed files with 23 additions and 14 deletions

View File

@ -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

View File

@ -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):

View File

@ -1,14 +1,17 @@
<!-- templates/signup.html -->
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% load i18n %}
{% block title %}Sign Up{% endblock %}
{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{{ user_form|crispy }}
{{ form|crispy }}
<button type="submit">Sign up</button>
{% csrf_token %}
{{ form|crispy }}
{{ user_form|crispy }}
<button class="btn btn-link" type="submit">
{% trans "Sign Up" %}
</button>
</form>
{% endblock %}