0

I'm trying to update my webapp2 router to take me to a different handler in a certain case here is what I have:

address: "*.com/blog/post?action=edit&id=fl2j3r0udflj3"

to keep it simple I want to just match on /blog/post?action=edit

I have this:

app = webapp2.WSGIApplication([
    ('/',Redirect),
    (r'/blog/post?(\S+)', blog.EditPost),
    ('/blog/post', NewPost),
....

doesn't work.

I've tried several different things such as

  1. ('r/blog/post\?action=edit', blog.EditPost)
  2. ('r/blog/post\?action=edit(.*+)', blog.EditPost)

Nothing is working. Any ideas?

3
  • 1
    ?... is a query string, which is not part of the path. Get the action argument in your handler and call the action function. Commented Feb 5, 2016 at 11:17
  • 1
    Why are you constructing the URLs like that? It would be more idiomatic to use /blog/post/<id>/edit. Commented Feb 5, 2016 at 12:29
  • And use webapp2 extended routing to match the /blog/post/<id>/edit request: webapp-improved.appspot.com/guide/routing.html#extended-routes Commented Feb 5, 2016 at 14:49

1 Answer 1

1

tl;dr: If you want to maintain your URL structure, your '/blog/post' handler is going to need to dispatch further based on presence parameters and their values.

This is one of those situations were it helps to have prior knowledge of what's meant by a 'path' when talking about URLs. Specifically, that parameters aren't part of the path.

It's also solvable by digging in to the SDK source, with the goal of answering the question "which part of the URL is being considered when matching routes?"

webapp2 provides functionality on top of webob. webapp2.WSGIApplication builds a list of webapp2.SimpleRoutes for the routes passed to the WSGIApplication constructor. A SimpleRoute "matches" based on request.path, which is a property on the request provided by webob.Request (which webapp2.Request subclasses). The path property starts with

    @property
    def path(self):
        """The path of the request, without host or query string"""

Meaning, ultimately, that query strings aren't matchable by routes. (You could dig further in to code to confirm that.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I'm realizing I should just restructure my classes to be more broadly applicable instead of creating a new handler for each regex I might encounter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.