Spending hours debugging new errors after splitting a big controller into
two files. It seems like web2py is not robust with multiple controllers in
a single applications.
Suddenly, Python thinks that I can't use the format method on a string.
In a form to edit categories, a user can press a submit button named delete
to delete a category. If the category still reference items in the
database, I generate a flash to tell the user and don't do the delete.
Here is the part of the form code:
cats_view = TABLE()
for cat in cats:
cats_view.append(
TR(
TD(cat.name),
TD(
FORM(INPUT(_type="hidden", _name="id", _value=cat.id),
INPUT(_type="hidden", _name="name",
_value=cat.name),
INPUT(_type="submit", _name="edit", _value="edit"),
INPUT(_type="submit", _name="edit",
_value="delete"),
_onsubmit="return delete_confirm(this);"
)
)
)
)
if cats.__len__() == 0:
cats_view = "Well, you don't seem to have any categories to edit..."
form = FORM(cats_view, _onsubmit="return delete_confirm(this);")
if request.post_vars:
cat_name = request.post_vars.name
cat_num = request.post_vars.id
if request.post_vars.edit == "edit":
form.insert(0, FORM('Category: ',
INPUT(_type = "hidden", _name = "id", _value =
cat_num),
INPUT(_name = 'new_name', _value = cat_name),
XML(' '),
INPUT(_type = 'submit'))
)
elif request.post_vars.edit == "delete":
cat_joke_cnt = jodb(jodb.joke_category.category ==
cat_num).count()
if cat_joke_cnt > 0:
session.flash = (
"""The category {0} still has {1} jokes. You can't
delete
a category that contains jokes.""".format(cat_name,
cat_joke_cnt)
)
redirect(URL('full', 'full_view'))
elif cat_name == 'not sure':
session.flash = """So sorry. You can't delete the '{0}'
category.""".format(cat_name)
redirect(URL('full', 'full_view')) # prevents inconsistent
id for category "not sure"
else:
id = jodb(jodb.category.id == cat_num).delete()
session.flash = "Category " + cat_name + " deleted."
redirect(URL('full', 'full_view'))
After attempting to delete a category that still references items, the
following error resulted:
<type 'exceptions.AttributeError'> 'str' object has no attribute 'format'
VERSIONweb2py™(2, 2, 1, datetime.datetime(2012, 10, 21, 16, 57, 4),
'stable')TRACEBACK
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
Traceback (most recent call last):
File "C:\Users\Lewisl\Dropbox\web2py\gluon\restricted.py", line 212, in
restricted
exec ccode in environment
File
"C:/Users/Lewisl/Dropbox/web2py/applications/pyjokes/controllers/full.py"
<http://127.0.0.1:8000/admin/default/edit/pyjokes/controllers/full.py>, line
573, in <module>
File "C:\Users\Lewisl\Dropbox\web2py\gluon\globals.py", line 188, in <lambda>
self._caller = lambda f: f()
File "C:\Users\Lewisl\Dropbox\web2py\gluon\tools.py", line 2911, in f
return action(*a, **b)
File "C:\Users\Lewisl\Dropbox\web2py\gluon\tools.py", line 2911, in f
return action(*a, **b)
File
"C:/Users/Lewisl/Dropbox/web2py/applications/pyjokes/controllers/full.py"
<http://127.0.0.1:8000/admin/default/edit/pyjokes/controllers/full.py>, line
467, in edit_cat
a category that contains jokes.""".format(cat_name, cat_joke_cnt)
AttributeError: 'str' object has no attribute 'format'
All I did today was paste the cut and paste the function from default.py to
full.py. Previously the function worked perfectly.
A bug in web2py? Something I did?
Should I give up on multiple controllers and go back to just one file?
Thanks.
--