Python Forum
Flask - adding new page affects all other pages
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask - adding new page affects all other pages
#11
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()?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#12
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.
Reply
#13
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#14
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.
Reply
#15
I mean, you don't use data, just delete it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#16
Thanks Buran,

Yes you are correct, I have removed it and its working fine without it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask and SQLAlchemy question: Database is being created but tables aren't adding pythonpaul32 3 4,652 Feb-07-2023, 10:48 AM
Last Post: pythonpaul32
  Flask run function in background and auto refresh page raossabe 2 7,527 Aug-20-2022, 10:00 PM
Last Post: snippsat
  Reload flask current page GrahamL 2 5,131 Jan-08-2021, 08:31 AM
Last Post: GrahamL
  API auto-refresh on HTML page using Flask toc 2 11,845 Dec-23-2020, 02:00 PM
Last Post: toc
  [Flask]After login page is not redirecting me to dashboard shockwave 0 2,705 May-07-2020, 05:22 PM
Last Post: shockwave
  use Xpath in Python :: libxml2 for a page-to-page skip-setting apollo 2 3,630 Mar-19-2020, 06:13 PM
Last Post: apollo
  Flask - Opening second page via href is failing - This site can’t be reached rafiPython1 2 5,476 Apr-11-2018, 08:41 AM
Last Post: rafiPython1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020