I have following piece of code:
render = web.template.render('templates/')
app = web.application(urls, globals())
I read about template imports in the web.py cookbook.
Now, when I try to import re in a template:
render = web.template.render('templates/', globals={'re':re})
app = web.application(urls, globals())
I got an error:
<type 'exceptions.TypeError'> at /'dict' object is not callable
and this line showed in the traceback: app = web.application(urls, globals()).
But when I modify it:
app = web.application(urls)
the error is gone, and re is imported in my template.
I don't understand how globals={'re': re} in web.template.render breaks it?
Why I can't keep both globals as in second example?
I'm guessing there is something else you are doing in your script or template that is causing that error. If you showed the complete example, then it would be easier to see. Here is a working example:
import web
import re
urls = ('/', 'index')
render = web.template.render('templates/', globals={'re':re})
app = web.application(urls, globals())
class index:
def GET(self):
args = web.input(s='')
return render.index(args.s)
if __name__ == '__main__':
app.run()
And the template, index.html:
$def with(s)
$code:
if re.match('\d+', s):
num = 'yes'
else:
num = 'no'
<h1>Is arg "$:s" a number? $num!</h1>
Browse to http://localhost:8080/?s=123 to try it.
Related
I have a catch-all route set up in Flask and I want to parse the URL regardless of the length.
from flask import Flask
app = Flask(__name__)
app.route('/')
app.route('/<path:path>')
def main(path=None):
if path == None:
return 'foo'
else:
return 'bar'
if __name__ == '__main__':
app.run()
The problem is I'm getting a 404 Not Found error and I don't know why. The url I'm using to test is /hello/world/. Thanks in advance.
You forgot # before routing decorators. Change it this way:
#app.route('/')
#app.route('/<path:path>')
and it will work.
I am trying to add a function in the Jinja environment from a blueprint (a function that I will use into a template).
Main.py
app = Flask(__name__)
app.register_blueprint(heysyni)
MyBluePrint.py
heysyni = Blueprint('heysyni', __name__)
#heysyni.route('/heysyni'):
return render_template('heysyni.html', heysini=res_heysini)
Now in MyBluePrint.py, I would like to add something like :
def role_function():
return 'admin'
app.jinja_env.globals.update(role_function=role_function)
I will then be able to use this function in my template. I cannot figure out how I can access the application since
app = current_app._get_current_object()
returns the error:
working outside of request context
How can I implement such a pattern ?
The message error was actually pretty clear :
working outside of request context
In my blueprint, I was trying to get my application outside the 'request' function :
heysyni = Blueprint('heysyni', __name__)
app = current_app._get_current_object()
print(app)
#heysyni.route('/heysyni/')
def aheysyni():
return 'hello'
I simply had to move the current_app statement into the function. Finally it works that way :
Main.py
from flask import Flask
from Ablueprint import heysyni
app = Flask(__name__)
app.register_blueprint(heysyni)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
Ablueprint.py
from flask import Blueprint, current_app
heysyni = Blueprint('heysyni', __name__)
#heysyni.route('/heysyni/')
def aheysyni():
# Got my app here
app = current_app._get_current_object()
return 'hello'
So I'm getting into the process of putting some of my first python code online and I'm a little fuzzy about some things. When we assign app to web.application(urls, globals()), what is going on exactly? Also, the line form = web.input(name="Nobody", greet=None) is referring to the two input forms in my other script called hello_form, but what is its purpose here? We're calling form.greet and form.name on the next line I see, but those should be variables created based on user input, (yet we say name = "Nobody"?).
import web
urls = (
'/hello', 'Index'
)
app = web.application(urls, globals())
render = web.template.render('templates/', base="layout")
class Index(object):
def GET(self):
return render.hello_form()
def POST(self):
form = web.input(name="Nobody", greet=None)
greeting = "%s, %s" % (form.greet, form.name)
return render.index(greeting = greeting)
if __name__ == "__main__":
app.run()
app = web.application(urls, globals())
creates a variable named app and stores the result of calling web.application() in it. In other words, you create a web application object, and store that object in app.
form = web.input(name="Nobody", greet=None)
The values specified for name and greet here are the default values - namely, those used if no user specified values are provided in the request.
I'm testing web.py and forms but I cant get any value in return. This is the code:
import web
from web import form
class add:
def GET(self):
f = login()
return render.formtest(f)
def POST(self):
f = login()
print f["ip"].value
return render.formpost(f)
render = web.template.render('templates/')
login = form.Form(
form.Textbox("ip", id="ip"),
form.Textbox('snmp_community'),
)
urls = ( '/','index', '/add', 'add')
app = web.application(urls,globals())
if __name__ == "__main__": app.run()
I followed this example: http://webpy.org/form but when I print the value of f["ip"].value or f.d.ip I always get "None".
Thank you for the help.
Here is a line from the web.py doc:
Note: You cannot access form values before having validated the form!
so you'll have to call f.validates() before you can access the posted data.
Ive recently gotten into Webdesign in Python, I've tried multiple frameworks but web.py seems to be my favorite except for one problem. I cant seem to figure out how to make multiple pages with multiple templates....
here is my code so far:
import web
urls = (
'/', 'index', '/login/', 'login'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class index():
def GET(self):
return render.index()
class login():
def GET(self):
return render.login()
if __name__ == '__main__':
app.run()
I get an error when I try to go to the login page :/
Try changing your url mapping:
urls = (
'/', 'index',
'/login/?', 'login',
)
/login/? will work for /login and /login/ url paths.
It will be better if you show an exception that you get.