Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes a bottle route for /something also handle /something/else?
    primarykey
    data
    text
    <p>I have a <a href="http://bottlepy.org/docs/dev/index.html" rel="nofollow">bottle</a> site that loads content via <code>getJSON()</code> requests. </p> <p>To handle normal requests made when navigating the site, <code>getJSON()</code> requests are made to a Python script which dumps the results in JSON format. </p> <p>Push/pop state is used to change the URL when the dynamic content is loaded. </p> <p>To handle this same dynamic content when directly accessing the URL, eg reloading the page, I create a conditional which loads the bottle template and passes the path to the <code>getJSON()</code> request, which then loads the dynamic content. </p> <pre><code>@route('/') @route('/&lt;identifier&gt;') def my_function(identifier='index'): # do things with certain paths using if and elif conditionals # # handle the getJSON() path elif value_passed_to_getJSON.startswith("part_one/"): # get the path after / , in this example it should be 'part_two' my_variable = value_passed_to_getJSON.split("/")[1] # perform database query with my_variable response.content_type = 'application/json' return dumps(cursor) # and later for direct URL access, eg reloading the page, this essentially routes the query # back to the getJSON() path handler above. elif identifier == "part_one/part_two": return template('site_template',value_passed_to_getJSON="/" + identifier) </code></pre> <p>This setup is working when the identifier is something like <code>part_one</code> but not in the format above ie <code>part_one/part_two</code>, in that case a 404 is raised. </p> <p>As another test, if I simply put:</p> <pre><code> elif identifier == "part_one/part_two": return "hello" </code></pre> <p>I also get a 404 with a Firebug error on <code>GET part_two</code>. </p> <p>I am wondering if this is because the initial route <code>@route('/&lt;identifier&gt;')</code> only contains a single value and forward slash? </p> <p>Does it need an extra wildcard to handle the two parts of the path?</p> <p><strong>Demonstrated Solution</strong> (per comment below)</p> <pre><code>@route('/') @route('/&lt;identifier:path&gt;') @view('my_template.tpl') def index(identifier='index'): if identifier == 'part_one/part_two': return "hello" else: return "something standard" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload