I attempting to to run a python script inside another python script, upon entering a webpage using Django framework.
This is my views script that is running the python script tech_scripts.py
import subprocess
from django.shortcuts import render
from .models import ConfigInfo
from .forms import ConfigInfoForm
from .models import dhcp
from .forms import dhcpForm
def post_list_three(request):
from .models import ConfigInfo
ConfigInfo.objects.all().delete()
if request.method =="POST":
form =ConfigInfoForm(request.POST)
if form.is_valid():
ConfigInfo=form.save(commit=False)
ConfigInfo.save()
subprocess.Popen(["python3", "/home/pi/tutorial/device_interfaces/device_interfaces/tech_scripts.py","-m"])
else:
form=ConfigInfoForm()
return render(request, 'device_interfaces/post_edit.html', {'form':form})
So, upon opening a webpage i want the function post_list_three to be executed. The subprocess manages to run the tech_scripts file which is below
#!/usr/bin/env python3
import json
import re
import os.path
import pathlib
import uuid
import subprocess
from models.py import ConfigInfo
def info():
creds=ConfigInfo.objects.first()
SSID_name=creds.SSID
psk_name=creds.psk
sIP=creds.sIP
netmask=creds.netmask
gateway=creds.gateway
netwrokIP=creds.networkIP
broadcast=creds.broadcast
dns_servers=creds.dns_servers
config['ssid']=getattr(creds,SSID_name)
config['password']=getattr(creds,psk_name)
config['StatIP']=getattr(creds,sIP)
config['Netmask']=getattr(creds,netmask)
config['gateway']=getattr(creds,gateway)
config['Network']=getattr(creds,networkIP)
config['Broadcast']=getattr(creds,broadcast)
config['DNS']=getattr(creds,dns_servers)
jstring = json.dumps(config)
with open("/home/techuser/config.json", "w+") as handle:
handle.write(jstring)
However, when going to my website after launching it using
python3 manage.py runserver 192.168.1.150:8000
I get a traceback ,
[03/Dec/2018 09:51:24] "POST /post/new/static HTTP/1.1" 200 1884
Traceback (most recent call last):
File "/home/pi/tutorial/device_interfaces/device_interfaces/tech_scripts.py", line 9, in <module>
from models.py import ConfigInfo
File "/home/pi/tutorial/device_interfaces/device_interfaces/models.py", line 7, in <module>
class ConfigInfo(models.Model):
File "/home/pi/tutorial/device_interfaces/myvenv/lib/python3.5/site-packages/django/db/models/base.py", line 87, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/pi/tutorial/device_interfaces/myvenv/lib/python3.5/site-packages/django/apps/registry.py", line 249, in get_containing_app_config
self.check_apps_ready()
File "/home/pi/tutorial/device_interfaces/myvenv/lib/python3.5/site-packages/django/apps/registry.py", line 131, in check_apps_ready
settings.INSTALLED_APPS
File "/home/pi/tutorial/device_interfaces/myvenv/lib/python3.5/site-packages/django/conf/__init__.py", line 57, in __getattr__
self._setup(name)
File "/home/pi/tutorial/device_interfaces/myvenv/lib/python3.5/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/home/pi/tutorial/device_interfaces/myvenv/lib/python3.5/site-packages/django/conf/__init__.py", line 107, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named 'mysite'
The traceback highlights that there is something wrong with the line
from models.py import ConfigInfo
Attempts, but failed
from .models import ConfigInfo
import ConfigInfo
from models import ConfigInfo
My models.py below
from django.db import models
from django.conf import settings
from django.utils import timezone
class ConfigInfo(models.Model):
SSID = models.CharField(max_length=50)
psk = models.CharField(max_length=50)
sIP = models.CharField(max_length=20)
netmask = models.CharField(max_length=20)
gateway = models.CharField(max_length=20)
networkIP=models.CharField(max_length=20)
broadcast=models.CharField(max_length=20)
dns_nameservers=models.CharField(max_length=20)
How can i fix this issue?
Comments
Post a Comment