Last active
July 10, 2016 10:47
-
-
Save HopHouse/46cfa0ef9eab8d12d196aa31d00ee656 to your computer and use it in GitHub Desktop.
inline_formset in django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "staff_base.html" %} | |
{% load bootstrap3 %} | |
{% block title %}Salamander{% endblock %} | |
{% block content_title %} | |
<h1 class="page-header">Create coach</h1> | |
{% endblock %} | |
{% block content %} | |
<div class="row sub-header"> | |
<h2 class="col-md-4"> | |
Create a coach | |
</h2> | |
</div> | |
<div class = "row"> | |
</br> | |
<div class="col-md-5"> | |
<form action="" method="POST"> | |
{% csrf_token %} | |
{%bootstrap_form form%} | |
</div> | |
<div class="col-md-5 col-md-offset-1"> | |
{{ coach_item_form.management_form }} | |
{{ coach_item_form.non_form_errors }} | |
{% for form in coach_item_form %} | |
{%bootstrap_form form%} | |
{% endfor %} | |
</div> | |
</div> | |
<div class = "row"> | |
<div class = "col-md-12" style = "text-align:cneter" > | |
<input type="submit" class="btn btn-primary center-block" value="Envoyer"/> | |
</form> | |
</div> | |
</div> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.forms.models import inlineformset_factory | |
from staffManagement.models import MyUser, Coach | |
class MyUserForm(forms.ModelForm): | |
class Meta: | |
model = MyUser | |
fields = ['first_name', 'last_name', 'email', 'street_number', 'street', 'zip_code', 'city', 'phone_number', 'birthday'] | |
CoachFormSet = inlineformset_factory(MyUser, Coach, can_delete=False, | |
fields = ('weight', 'height', 'first_position', 'second_position', | |
'number', 'emergency_first_name', 'emergency_last_name', | |
'emergency_function', 'emergency_phone')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from staffManagement.models import Coach, Player | |
from staffManagement.models import MyUser as Member | |
from staffManagement.forms import * | |
from django.views.generic import CreateView | |
class CoachCreateView(WithUserMixin, CreateView): | |
"""Create a coach | |
""" | |
model = MyUser | |
form_class = MyUserForm | |
template_name = "staffManagement/coach_create.html" | |
success_url = "/staff/" | |
def get(self, request, *args, **kwargs): | |
self.object = None | |
form_class = self.get_form_class() | |
form = self.get_form(form_class) | |
coach_item_form = CoachFormSet() | |
return self.render_to_response(self.get_context_data(form = form, coach_item_form = coach_item_form)) | |
def post(self, request, *args, **kwargs): | |
self.object = None | |
form_class = self.get_form_class() | |
form = self.get_form(form_class) | |
coach_item_form = CoachFormSet(self.request.POST) | |
if form.is_valid() and coach_item_form.is_valid(): | |
return self.form_valid(form, coach_item_form) | |
else: | |
return self.form_invalid(form, coach_item_form) | |
def form_valid(self, form, coach_item_form): | |
self.object = form.save(commit = False) | |
self.object.team = self.request.user.team | |
salt = os.urandom(2).encode('hex') | |
username = str(self.object.last_name + self.object.first_name + | |
self.object.team.name + salt) | |
self.object.username = username | |
self.object.save() | |
coach_item_form.instance = self.object | |
coach_item_form.save() | |
return HttpResponseRedirect(self.get_success_url()) | |
def form_invalid(self, form, coach_item_form): | |
return self.render_to_response(self.get_context_data(form = form, coach_item_form = coach_item_form)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment