Python Forum

Full Version: Flask - adding new page affects all other pages
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
both router.addvlan() and router.addinterface() methods don't have explicit return, so they return None.
How does it render correctly anything when you pass None as result argument in render_template()?
Hi Buran,

Thanks again for your help, this is what I did and it works great.
My classes being the same name were interfering with the functions.

from flask import render_template, redirect, Flask, request
from cisco import router
from addvlan import routervlan
from addinterface import routerinterface
from netmiko import ConnectHandler
import textfsm
@app.route('/addvlan',methods = ['POST', 'GET'])
def addvlan():
        if request.method == 'POST':
                result = request.form.to_dict()
                data = request.form.to_dict('number')
                addvlan = routervlan('cisco_ios', result['hostname'], result['username'], result['password'], result['port'], result['number'], result['vlanname'])
                return render_template('addvlan.html',result=addvlan.addvlan())
        else:
                return render_template('addvlan.html')
In addplan.py I changed the class to match and completed the same in addvlan.
from netmiko import ConnectHandler
import textfsm

class routervlan(object):
I am delighted thanks again.
Not tested, but something like this is what I mean:
class Router:
    def __init__(self,device_type,ip,username,password,port):
        self.device_type = device_type
        self.ip = ip
        self.username = username
        self.password = password
        self.port = port
        # you can actually skip following 4 lines
        self.number = None
        self.vlanname = None
        self.datavlan = None
        self.voicevlan = None
 
 
    def _connect(self):
        self.net_connect = ConnectHandler(device_type=self.device_type, ip=self.ip, 
                                            username=self.username, password=self.password, 
                                            port=self.port)
        self.net_connect.send_command('en')
        self.net_connect.send_command('cisco')
 
 
    def add_vlan(self, number,vlanname):
        # not sure you need to keep these 2 as properties of the class
        self.number = number
        self.vlanname = vlanname
 
        self._connect()
         
        # if you can use f-strings instead
        # config_commands = [f'vlan {self.number}', f'name {self.vlanname}']
        config_commands = [
                'vlan {number}'.format(number=self.number),
                'name {vlanname}'.format(vlanname=self.vlanname)]
 
        return self.net_connect.send_config_set(config_commands)
 
 
    def add_interface(self, interface, datavlan, voicevlan):
                 
        # not sure you need to keep these 3 as properties of the class
        self.interface = interface
        self.datavlan = datavlan
        self.voiceclan = voicevlan
 
        self._connect()
 
        # if you can use f-strings instead
        # config_commands = [f'interface gi{self.interface}', 'switchport', 
        # f'switchport access vlan {self.datavlan}', f'switchport voice vlan {self.voicevlan}']
        config_commands = [
                'interface gi{interface}'.format(interface=self.interface),
                'switchport',
                'switchport access vlan {datavlan}'.format(datavlan=self.datavlan),
                'switchport voice vlan {voicevlan}'.format(voicelan=self.vlan)]
                 
        return net_connect.send_config_set(config_commands)
But basically, it can be shortened further to:

class Router:
    def __init__(self,device_type,ip,username,password,port):
        self.device_type = device_type
        self.ip = ip
        self.username = username
        self.password = password
        self.port = port 
 

    def _connect(self):
        self.net_connect = ConnectHandler(device_type=self.device_type, ip=self.ip, 
                                            username=self.username, password=self.password, 
                                            port=self.port)
        self.net_connect.send_command('en')
        self.net_connect.send_command('cisco')
 
 
    def add_vlan(self, number,vlanname):
        self._connect()
        config_commands = [f'vlan {number}', f'name {vlanname}'] 
        return self.net_connect.send_config_set(config_commands)
 
 
    def add_interface(self, interface, datavlan, voicevlan): 
        self._connect()
        config_commands = [f'interface gi{interface}', 'switchport', 
                           f'switchport access vlan {datavlan}', 
                           f'switchport voice vlan {voicevlan}']
        return net_connect.send_config_set(config_commands)
Then you can have one Router class. And as I said your methods should return someting, not None, that you pass as result argument in render_template(). That is if you want to display the result, of course.

By the way, not sure why you have this line data = request.form.to_dict('number'). You only work with result.
Thanks Buran

I will try that it would simplify my script rather than having a separate script for each.

The line
Output:
data = request.form.to_dict('number')
is strange, I am not good at Python as you have probably guessed. Leaving it empty, just () throw up errors, any variable I put in erred, apart from when I put in the first variable, no errors. It is bad programming I know but it worked.
I mean, you don't use data, just delete it
Thanks Buran,

Yes you are correct, I have removed it and its working fine without it.
Pages: 1 2