bradtraversy / myflaskapp

Python Flask app with authentication
MIT License
335 stars 264 forks source link

jinja2.exceptions.UndefinedError #12

Open vasudev430 opened 5 years ago

vasudev430 commented 5 years ago

Getting below error: jinja2.exceptions.UndefinedError: 'form' is undefined

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\flask\app.py", line 2309, in __call__

return self.wsgi_app(environ, start_response)

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\flask\app.py", line 2295, in wsgi_app

response = self.handle_exception(e)

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\flask\app.py", line 1741, in handle_exception

reraise(exc_type, exc_value, tb)

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\flask\_compat.py", line 35, in reraise

raise value

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\flask\app.py", line 2292, in wsgi_app

response = self.full_dispatch_request()

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request

rv = self.handle_user_exception(e)

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\flask\app.py", line 1718, in handle_user_exception

reraise(exc_type, exc_value, tb)

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\flask\_compat.py", line 35, in reraise

raise value

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request

rv = self.dispatch_request()

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\flask\app.py", line 1799, in dispatch_request

return self.view_functions[rule.endpoint](**req.view_args)

File "C:\Users\IBM_ADMIN\py\myflaskapp\app.py", line 73, in register

return render_template('register.html')

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\flask\templating.py", line 135, in render_template

context, ctx.app)

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\flask\templating.py", line 117, in _render

rv = template.render(context)

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\jinja2\asyncsupport.py", line 76, in render

return original_render(self, *args, **kwargs)

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\jinja2\environment.py", line 1008, in render

return self.environment.handle_exception(exc_info, True)

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\jinja2\environment.py", line 780, in handle_exception

reraise(exc_type, exc_value, tb)

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\jinja2\_compat.py", line 37, in reraise

raise value.with_traceback(tb)

File "C:\Users\IBM_ADMIN\py\myflaskapp\templates\register.html", line 1, in top-level template code

{% extends 'layout.html' %}

File "C:\Users\IBM_ADMIN\py\myflaskapp\templates\layout.html", line 12, in top-level template code

{%block body %} {% endblock %}

File "C:\Users\IBM_ADMIN\py\myflaskapp\templates\register.html", line 8, in block "body"

{{render_field(form.name, class_="form-control")}}

File "C:\Users\IBM_ADMIN\Anaconda2\envs\Flask\lib\site-packages\jinja2\environment.py", line 430, in getattr

return getattr(obj, attribute)

jinja2.exceptions.UndefinedError: 'form' is undefined

The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.

To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

dump() shows all variables in the frame
dump(obj) dumps all that's known about the object
zealrods commented 2 years ago

solution: after watching the video u might have done like this

USER REGISTERATION

@app.route('/register',methods =['GET','POST']) def register(): form = RegisterForm(request.form) if request.method =='POST' and form.validate(): name = form.name.data email = form.email.data username = form.username.data password = sha256_crypt.encrypt(str(form.password.data))

    # create cursor
    cur = mysql.connection.cursor()

    cur.execute("INSERT INTO users(name,email,username,password) VALUES(%s,%s,%s,%s)",(name,email,username,password))

    # commit to Database
    mysql.connection.commit()

    # close connection 
    cur.close()
    flash('you are now registered and can login ','success')

    redirect(url_for('index'))

    return render_template('register.html')
return render_template('register.html',form = form)

##########################################################################################

now instead of doing like that just do like this ##########################################################################################

USER REGISTERATION

@app.route('/register',methods =['GET','POST']) def register(): form = RegisterForm(request.form) if request.method =='POST' and form.validate(): name = form.name.data email = form.email.data username = form.username.data password = sha256_crypt.encrypt(str(form.password.data))

    # create cursor
    cur = mysql.connection.cursor()

    cur.execute("INSERT INTO users(name,email,username,password) VALUES(%s,%s,%s,%s)",(name,email,username,password))

    # commit to Database
    mysql.connection.commit()

    # close connection 
    cur.close()
    flash('you are now registered and can login ','success')

    redirect(url_for('index'))

    return render_template('register.html',form = form)
return render_template('register.html',form = form)