Embarrassing question time again: NameError: name 'post' is not defined

320 views
Skip to first unread message

mkt...@gmail.com

unread,
Dec 10, 2020, 6:17:31 PM12/10/20
to web2py-users
While redoing my own damn tutorial I'm running into a problem that has to be painfully obvious to anyone on the world but me. I get the message "NameError: name 'post' is not defined" when I try to do a /new in the following conditions. What idiocy am I committing here?

file models.py:
from .common import db, Field
from pydal.validators import *

db.define_table('post',  
    Field('title','string',notnull=True),
    Field('url', 'text',IS_URL()),
    Field('body','string'))

db.commit()

new.html:
[[extend 'layout.html']]
<h1>[[=post.title]]</h1>
<p>[[=post.body]]</p>
[[=A('Home', _href=URL('index'))]]

post.html:
[[extend 'layout.html']]
<h1>[[=post.title]]</h1>
<p>[[=post.body]]</p>
[[=A('Home', _href=URL('index'))]]

In controllers.py:
@action("post/<id>")
@action.uses('post.html')
def post(id):
    post=db.post(id)
    form=Form(db.post,post)
    return dict(post=post,form=form)

@action('new',method=['GET','POST'])    
@action.uses(auth,flash,session,'new.html')
def new():
    form=Form(db.post)
    if form.accepted:
        redirect(URL('index'))
    elif form.errors:
        flash.set('You\'ll need either a URL or a comment', class_="error")
    return dict(form=form)

villas

unread,
Dec 10, 2020, 7:12:48 PM12/10/20
to web2py-users
In your view new.html  file you are referencing a post record.  I suppose you should be displaying the form. [[=form]]
Just keep experimenting and note that if a valid record id from db.post in not found then it would evaluate as None.  This means that, for example,  post.title would then give an error in your views.

mkt...@gmail.com

unread,
Dec 10, 2020, 7:26:34 PM12/10/20
to web2py-users
Thanks a ton, villas! Hilariously I got it right in the tutorial but I was retyping everything by hand just to make sure and... yeah. Here, let me turn in my py4web card.

villas

unread,
Dec 11, 2020, 7:11:21 AM12/11/20
to web2py-users
You're welcome!
(Just one more thing,  you posted in the wrong group. But no prob!  Best wishes)

mkt...@gmail.com

unread,
Dec 11, 2020, 1:12:48 PM12/11/20
to web2py-users
D'oh! Sorry, beloved web2py people!
Reply all
Reply to author
Forward
0 new messages