1

Say I have two routes set up with Flask:

app.add_url_rule('/example', view_func = example.Index.as_view('example'))
app.add_url_rule('/example/<string:example_key>', view_func = example.Show.as_view('example'), methods=['GET'])

This routes /example to example.Index and /example/1 to example.Show.

When I go to /example?parameter=miau however it routes to example.Show instead of example.Index.

Why does this happen and how should I solve this?

1 Answer 1

1

Found out, mind the "example.Index.as_view('example')", it should be:

app.add_url_rule('/example', view_func = example.Index.as_view('example_index'))
app.add_url_rule('/example/<string:example_key>', view_func = example.Show.as_view('example_show'), methods=['GET'])
Sign up to request clarification or add additional context in comments.

1 Comment

Why this happened: the first parameter to as_view is the name of the generated view function, which is the default “endpoint”. Flask maps URL rules to endpoints, and then endpoints to view functions. In the first code sample both views had the same name so both rules had the same endpoint. The same view function (the latter) was used in both cases.

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.