def new(request): if request.method == 'POST': form = LayoutForm(request.POST) windows = WindowFormSet(request.POST, prefix='windows') doors = DoorFormSet(request.POST, prefix='doors') if form.is_valid() and windows.is_valid() and doors.is_valid(): layout = form.create(request) stories = form.cleaned_data['stories'] windows.create(layout) doors.create(layout) names = ['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth'] names = [n + ' Floor' for n in names] if stories > 2: names.insert(0, 'Basement') for (story, name) in enumerate(names[:stories]): Floor.objects.create(layout=layout, story=story, name=name) return redirect(layout) else: form = LayoutForm() windows = WindowFormSet(prefix='windows', queryset=Window.objects.none()) doors = DoorFormSet(prefix='doors', queryset=Door.objects.none()) return render(request, 'properties.hisp', { 'form': form, 'windows': windows, 'doors': doors, })
def properties(request, layout): from forms import UpdateLayoutForm as LayoutForm layout = get_object_or_404(Layout, user=request.user, pk=layout) if request.method == 'POST': form = LayoutForm(request.POST, instance=layout) windows = WindowFormSet(request.POST, prefix='windows') doors = DoorFormSet(request.POST, prefix='doors') if form.is_valid() and windows.is_valid() and doors.is_valid(): form.save() windows.save(layout) doors.save(layout) return redirect('users.views.home') else: form = LayoutForm(instance=layout) windows = WindowFormSet(prefix='windows', queryset=layout.windows.all()) doors = DoorFormSet(prefix='doors', queryset=layout.doors.all()) return render(request, 'properties.hisp', { 'layout': layout, 'form': form, 'windows': windows, 'doors': doors, })