diff --git a/app/main/views.py b/app/main/views.py index 867225c904a6b0cd55911ae379bc29798f5bdaad..7decb1948f3a9ce782a9eee415cab4d672a883d6 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -10,7 +10,7 @@ This module implements the main blueprint. """ import sqlalchemy as sa -from flask import (Blueprint, render_template, jsonify, +from flask import (Blueprint, render_template, jsonify, session, redirect, url_for, request, flash, current_app) from flask_login import login_required from .forms import AttributeForm, HostForm, ItemForm @@ -156,17 +156,17 @@ def list_hosts(): @bp.route('/hosts/create', methods=('GET', 'POST')) @login_groups_accepted('admin', 'create') def create_host(): - # Try to get the network_id from the URL parameters - # to display the form with the same selected network - # when reloading the page (redirect after submit) + # Try to get the network_id from the session + # to pre-fill the form with the same network try: - network_id = request.args['network_id'] + network_id = session['network_id'] except KeyError: # No need to pass request.form when no extra keywords are given form = HostForm() else: form = HostForm(request.form, network_id=network_id) if form.validate_on_submit(): + network_id = form.network_id.data host = models.Host(ip=form.ip.data, network_id=form.network_id.data, name=form.name.data, @@ -182,7 +182,9 @@ def create_host(): flash(f'{e}', 'error') else: flash(f'Host {host} created!', 'success') - return redirect(url_for('main.create_host', network_id=host.network_id)) + # Save network_id to the session to retrieve it after the redirect + session['network_id'] = host.network_id + return redirect(url_for('main.create_host')) return render_template('create_host.html', form=form)