How can I rewrite url for python:
http://localhost:8081/?page=1
to
http://localhost:8081/1
here is my code, but it's not working:
class MainPage(webapp.RequestHandler):
def get(self, page):
mypage = self.request.get('page')
self.response.headers['Content-Type'] = 'text/plain'
if mypage == "":
self.response.out.write('Hello, webapp World!')
else:
self.response.out.write('page is ' + mypage)
application = webapp.WSGIApplication([('/', MainPage),('/(\d+)', MainPage)], debug=True)
You can use regular expressions in your controller. It's not Apache-style URL rewriting per se, but it gets the job done. The rewritten parameter is passed as an argument to the handler.
class MyRequestHandler(webapp.RequestHandler):
def get(self, page):
self.response.headers['Content-Type'] = 'text/plain'
if not page:
self.response.out.write('Hello, webapp World!')
else:
self.response.out.write('page is ' + page)
url_map = [('/(\d+)', MyRequestHandler)]
application = webapp.WSGIApplication(url_map, debug=True)
See How to configure app.yaml to support urls like /user/<user-id>? for a similar application.
Assuming you're using webapp:
class Rewriter(webapp.RequestHandler):
def get(self):
self.redirect(self.request.get('page'))
application = webapp.WSGIApplication([('/', Rewriter)],)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
You've defined two mappings for your MainPage handler, one that will pass in no parameters ('/'), and one that will pass in one parameter ('/(\d+)'). Your handler, however, expects exactly one argument, named page.
You either need to use two different handlers, or supply a default value for the page argument, like this:
class MainPage(webapp.RequestHandler):
def get(self, page=None):
if not page:
self.redirect('/%s', self.request.get('page'))
return
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('page is ' + mypage)
For future reference, when you get a stacktrace, include it in your question! Saying "It's not working" and making us guess exactly what's going wrong isn't a good way to get useful answers to your question.
Related
Hello Stackoverflow Guru's!
I'm a complete newb, and I've got a question that I can't seem to find the answer to (hopefully because it's so simple nobody has bothered to ask).
I'm designing a website that has a bunch of recipes using google app engine. I'd like to be able to render a bunch of the recipe pages using one handler, because I plan of having lots of recipes later and I don't want to have to make a new handler for each one. My code is below:
import urllib2
import webapp2
import jinja2
import os
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self,template,**params):
t = JINJA_ENVIRONMENT.get_template(template)
return t.render(params)
def render(self,template,**kw):
self.write(self.render_str(template,**kw))
class MainHandler(Handler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('main.html')
self.response.write(template.render())
class RecipeHandler(Handler, recipe):
def get(self, recipe):
recipe_pages = {
'carbonara' : 'carbonara.html'
'burger' : 'burger.html'
}
if recipe in recipe_pages:
template = JINJA_ENVIRONMENT.get_template(recipe_pages[recipe])
self.response.write(template.render())
else:
self.abort(404)
app = webapp2.WSGIApplication([
('/', MainHandler),
('/carbonara', RecipeHandler(carbonara)),
('/burger',RecipeHandler(burger)),
], debug=True)
I basically want to avoid writing out a "CarbonaraHander" and "BurgerHandler", and just use "RecipeHandler" to render both pages. I know this should be possible, but I have no idea how to do it.
Any help is appreciated!
Edit: I think I should be using something called regular expressions? But I don't really understand how they need to be used in this case.
AFAIK you can't pass args to the handler, you need to extract them from the request. This is what I'd do (pushed it a bit further to directly use the template name in the URl routing):
class RecipeHandler(Handler):
def extract_template_name_from_request(self):
return self.request.path_info[9:] # strip leading '/recipes/' (or whatever else you need)
def get(self):
template_name = self.extract_template_name_from_request()
try:
template = JINJA_ENVIRONMENT.get_template(template_name)
except Exception:
# can't locate a template matching the requested path
self.abort(404)
return
# prepare the template values as needed
values = {'recipe': {'name': template_name[:-5]}} # just an example
try:
self.response.write(template.render(values))
except Exception:
# failure rendering the template
self.abort(500)
app = webapp2.WSGIApplication([
('/recipes/.*.html', RecipeHandler), # see extract_template_name_from_request()
('/.*', MainHandler),
], debug=True)
I would like to parse the URL upon load to see if it has any parameters. I'm just trying to set up a basic test to see if that's possible. What is the correct regex to send a url like http://example.com/?hiyall to ParamHandler?
class ParamHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write('parameters detected')
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write('Hello World')
application = webapp2.WSGIApplication ([('/', MainHandler), ('/\?.*', ParamHandler)], debug=True)
Do something like this in get(self):
class ParamHandler(webapp2.RequestHandler):
def get(self):
hyl = self.request.get("hiyall")
self.response.out.write("hiyall was: " + hyl)
Customary official docs link: https://developers.google.com/appengine/docs/python/tools/webapp/requestclass#Request_get
If you're using webapp2, you cannot route requests based on parameters.
But you can create a condition based on query_string which can check if parameters exist. Like the following:
class MainHandler(webapp2.RequestHandler):
def get(self):
if self.request.query_string:
self.response.out.write('Has parameters')
else:
self.response.out.write('No parameters')
application = webapp2.WSGIApplication ([('/', MainHandler)], debug=True)
I just started using Tornado. All I would like to do is click the submit button on index and be sent to login via the form action (this works so far); however, I am having trouble getting the posted data to login and making it work.
First question is, why am I getting the error:
password = self.get_argument('password', None)
NameError: name 'self' is not defined
Here's the python so far:
import os
import tornado.web
import tornado.ioloop
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
class LoginHandler(tornado.web.RequestHandler):
def post(self):
useremail = self.get_argument('useremail', None)
password = self.get_argument('password', None)
self.render("login.html")
def main():
application = tornado.web.Application([
(r"/", MainHandler),
(r"/login", LoginHandler),
(r"/css/(.*)", tornado.web.StaticFileHandler, {"path": os.path.join(os. path.dirname(__file__), 'css')}),
(r"/pictures/(.*)", tornado.web.StaticFileHandler, {"path": os.path.join(os.path.dirname(__file__), 'pictures')}),
])
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
And secondly, is there anything else that looks glaringly wrong here? I am very uncertain as to how I'm supposed to render webpages like Login.html vs. Index.html, etc.
Syntactically, this seems to work also: return self.write(open("index.html", 'r').read())
What exactly does that do versus what I currently have?
Thanks for the help!
Your indentation is goofed, mixing spaces and tabs. Use python -tt to verify.
also if you do
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
def post(self):
self.render("index.html")
it will serve index.html for POST too.
.
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.
I want to build a REST web service on app engine. Currently i have this:
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
class UsersHandler(webapp.RequestHandler):
def get(self, name):
self.response.out.write('Hello '+ name+'!')
def main():
util.run_wsgi_app(application)
#Map url like /rest/users/johnsmith
application = webapp.WSGIApplication([(r'/rest/users/(.*)',UsersHandler)]
debug=True)
if __name__ == '__main__':
main()
And i would like to retreive for example all my users when the path /rest/users is accessed. I Imagine I can do this by building another handler, but I want to know if is possible to do it inside of this handler.
Sure, you can -- change your handler's get method to
def get(self, name=None):
if name is None:
"""deal with the /rest/users case"""
else:
# deal with the /rest/users/(.*) case
self.response.out.write('Hello '+ name+'!')
and your application to
application = webapp.WSGIApplication([(r'/rest/users/(.*)', UsersHandler),
(r'/rest/users', UsersHandler)]
debug=True)
In other words, map your handler to all the URL patterns you want it to handle, and make sure the handler's get method can distinguish among them easily (typically via its arguments).