Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WHY am I getting this NameError : name 'url_for' is not defined? [closed]

Tags:

python

flask

web

I am trying to build a URL following the instructions of the flask tutorial

http://flask.pocoo.org/docs/0.11/quickstart/

However I am keeping getting this NameError

name 'url_for' is not defined

THIS is the code :

from flask import Flask

app = Flask(__name__)

@app.route('/')
def Index() : pass

@app.route ('/ login')
def loGin() : pass

@app.route('/user/<username>')
def profile( username) : pass


with app.test_request_context() :
    print url_for('Index')
    print url_for('loGin')
    print url_for('loGin', next='/')
    print url_for('profile', username = 'Jon DD')

THIS is the total error message :

eloiim:minimalapp iivri.andre$ export FLASK_APP=Greet.py
eloiim:minimalapp iivri.andre$ flask run 
Traceback (most recent call last):
  File "/usr/local/bin/flask", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 478, in main
    cli.main(args=args, prog_name=name)
  File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 345, in main
    return AppGroup.main(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/click/core.py", line 696, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python2.7/site-packages/click/core.py", line 1060, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python2.7/site-packages/click/core.py", line 889, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python2.7/site-packages/click/core.py", line 534, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/usr/local/lib/python2.7/site-packages/click/core.py", line 534, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 388, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 124, in __init__
    self._load_unlocked()
  File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 148, in _load_unlocked
    self._app = rv = self.loader()
  File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 209, in load_app
    rv = locate_app(self.app_import_path)
  File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 89, in locate_app
    __import__(module)
  File "/Users/iivri.andre/Documents/minimalapp/Greet.py", line 16, in <module>
    print url_for('Index')
NameError: name 'url_for' is not defined

I am not sure what the issue is.

like image 428
siur Avatar asked Nov 29 '16 19:11

siur


People also ask

What is url_for in Python?

The url_for() function is very useful for dynamically building a URL for a specific function. The function accepts the name of a function as first argument, and one or more keyword arguments, each corresponding to the variable part of URL. The following script demonstrates use of url_for() function.

What does flask's url_for () do?

url_for in Flask is used for creating a URL to prevent the overhead of having to change URLs throughout an application (including in templates). Without url_for , if there is a change in the root URL of your app then you have to change it in every page where the link is present.


1 Answers

Add it to your import line:

from flask import Flask, url_for
like image 120
abigperson Avatar answered Sep 21 '22 19:09

abigperson