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 from crispy_forms.layout import Layout
class SignUpForm(UserCreationForm):
class Meta:
model = User
fields = ['first_name','last_name','username','email']
class ProfileForm(forms.ModelForm): 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: class Meta:
model = Profile model = Profile

View File

@ -16,7 +16,7 @@ from django_tables2.views import SingleTableView
from .models import Profile, Club, Membership 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 .tables import ClubTable,UserTable
from .filters import UserFilter, UserFilterFormHelper from .filters import UserFilter, UserFilterFormHelper
@ -29,10 +29,10 @@ class UserCreateView(CreateView):
Une vue pour inscrire un utilisateur et lui créer un profile Une vue pour inscrire un utilisateur et lui créer un profile
""" """
form_class = ProfileForm form_class = SignUpForm
success_url = reverse_lazy('login') success_url = reverse_lazy('login')
template_name ='member/signup.html' template_name ='member/signup.html'
second_form = UserCreationForm second_form = ProfileForm
def get_context_data(self,**kwargs): def get_context_data(self,**kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
@ -41,12 +41,12 @@ class UserCreateView(CreateView):
return context return context
def form_valid(self, form): def form_valid(self, form):
user_form = UserCreationForm(self.request.POST) profile_form = ProfileForm(self.request.POST)
if user_form.is_valid(): if form.is_valid() and profile_form.is_valid():
user = user_form.save() user = form.save()
user_profile = form.save(commit=False) # do not save to db profile = profile_form.save(commit=False)
user_profile.user = user profile.user = user
user_profile.save() profile.save()
return super().form_valid(form) return super().form_valid(form)
class UserDetailView(LoginRequiredMixin,DetailView): class UserDetailView(LoginRequiredMixin,DetailView):

View File

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