realpython / book2-exercises

Book 2 -- Exercises for the book
168 stars 203 forks source link

web2py section 22.11 Notes #56

Closed memilanuk closed 7 years ago

memilanuk commented 8 years ago

Trying to update the controller default.py with a function for the note(s) page... my code looks (as far as I can tell) the same as shown here in the repo, line 26 but I am getting an error ticket when I try to actually access the page:

File "/home/memilanuk/web2py/applications/py2manager/controllers/default.py", line 57, in note
  form = crud.create(db.note) if auth.user else "Login to Post to the Project"
NameError: global name 'crud' is not defined

Not sure if its a version thing; I'm using web2py 2.12.3 on pythonanywhere.com, vs. 2.8.2 as used in the book. Web2py is supposed to be pretty good about backwards compatibility, and I thought crud was a standard feature...?

stonemirror commented 8 years ago

I'm running into the same problem: I've tried replacing this line

form = crud.create(db.note) if auth.user else "Login to Post to the Project"

with

form = SQLFORM(db.note).process()

which makes the error go away, but when I click on the "Note" link in the index page's project list, it takes me to a blank page (at http://127.0.0.1:8000/note/1). Not sure why I'm not seeing anything inside the tag, my template looks okay...

(The "if" clause shouldn't be needed when the route is decorated with @auth.requires_login(), should it...?)

stonemirror commented 8 years ago

Okay, the blank page was me being bone-headed: my note() function was returning locals rather than locals().

With the substitution of a call to SQLFORM() for the call to crud.create() described in my last comment, this works fine and I can proceed on from here.

mjhea0 commented 8 years ago

Are you adding on the if as well:

form = SQLFORM(db.note).process() if auth.user else "Login to Post to the Project"
stonemirror commented 8 years ago

No, not so far, but I haven't tested it extensively, either yet. Is the if clause necessary when the route function is decorated with @auth.requires_login()? I'd reckoned that if I tried to get to the note/ URL without being logged in, I'd have gotten tossed back to the login screen. Why would a check like that be required here but in none of the other route functions...?

mjhea0 commented 7 years ago

Updated in v 2.0 of the courses. Coming week of 12/19/2016. https://github.com/realpython/about/blob/master/changelog.csv