jeudi 26 février 2015

I'm creating a python web interface. You complete the form, it sends it to a background python function, stuff happens.


I'm having trouble with one of these background functions, it's stumped me because it's not my first time creating a quick project like this, and I haven't encountered this error before.


I have three files: commands.py, header.py and product.py


product.py looks like this:



#!/usr/bin/python
import sys
import os
import cgitb
cgitb.enable()

sys.path.append(os.path.abspath("/var/www/cgi-bin/includes/"))
from header import *

page = get_page_name(sys.argv[0])

printDefaultPage(0,page)


and this is the route it takes through header.py:



def printDefaultPage(error,page): #this prints the page
printHTTPHeaders() #print headers
printHeader(page) #print header.inc
printPage(page)
printFooter() #print footer.inc


the above function prints out the headers, the head, the body of the page and the footer, all by calling other functions.



def printPage(page):
if page == "product":
print """<form method='post' id="register_form" name="client_frm"></br>
<label for="id">Enter the subscription to use:</label>"""
list_subscriptions()
print """<a href="/cgi-bin/index.py"><input class="login" type="button" value="Back"></a>
</form>"""


as you can see, printPage calls a function, list_subscriptions() which looks a bit like this:



def list_subscriptions():
subscriptions=get_subscriptions()
print """<select name = "sub">"""
for s in subscriptions:
id = s
price = subscriptions.get(str(s)).get('price')
scans = subscriptions.get(str(s)).get('scans_per_month')
print """<option value ="""+str(id)+"""">"""+str(price)+""" - """+str(scans)+"""</option>"""
print "</select>"


this in turn calls get_subscriptions, which exists in a file called commands, which is imported at the top:



#!/usr/bin/python
from commands import *


for completeness, here is get_subscriptions:



def get_subscriptions():
subscriptions=proxy.get_subscriptions()
return subscriptions


and on the other end:



def get_subscriptions(self):
subscriptions=self.store.find(Subscription)
subs = {}
for sub in subscriptions:
subs[str(sub.id)] = {}
subs[str(sub.id)]['scans_per_month'] = sub.scans_per_month
subs[str(sub.id)]['price'] = int(sub.price)
return subs


get_subscriptions() returns a dictionary. The more baffling thing is when I run printPage("product") in the python command line, it works.



Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from header import *
>>> printPage("product")
<form method='post' id="register_form" name="client_frm"></br>
<label for="id">Enter the subscription to use:</label>
<select name = "sub">
<option value =1">19 - 20</option>
<option value =3">59 - 100</option>
<option value =2">39 - 50</option>
<option value =5">0 - 0</option>
<option value =4">0 - 1000</option>
</select>
<a href="/cgi-bin/index.py"><input class="login" type="button" value="Back"></a>
</form>


Yet when I go to product.py in the browser expecting to see a nice select box instead I get this gross error:



/var/www/cgi-bin/includes/header.py in list_subscriptions()
7
8 def list_subscriptions():
=> 9 subscriptions=get_subscriptions()
10 print """<select name = "sub">"""
11 for s in subscriptions:
subscriptions undefined, get_subscriptions undefined




Aucun commentaire:

Enregistrer un commentaire