Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, first, you need your dev server to actually serve <code>main.js</code>, otherwise it won't be available for the browser.</p> <p>It's customary to put all <code>.js</code> and <code>.css</code> files under the <code>static</code> directory in small web apps, so your layout should look like this:</p> <pre><code> app.py - static/ main.js - views/ index.tpl </code></pre> <p>By no means this exact naming and layout is required, only often used.</p> <p>Next, you should supply a handler for the static files:</p> <pre><code>from bottle import static_file # ... @route('/static/:path#.+#', name='static') def static(path): return static_file(path, root='static') </code></pre> <p>This will actuall serve your files under <code>static/</code> to the browser.</p> <p>Now, to the last thing. You specified your JavaScript as:</p> <pre><code>&lt;script type="text/javascript" src="js/main.js" charset="utf-8"&gt;&lt;/script&gt; </code></pre> <p>That means the path to <code>.js</code> is <em>relative</em> to the current page. On you development server, the index page (<code>/</code>) will look for <code>.js</code> in <code>/js/main.js</code>, and another page (say, <code>/post/12</code>) will look for it in <code>/post/12/js/main.js</code>, and will sure fail.</p> <p>Instead, you need to use the <code>get_url</code> function to properly reference static files. Your handler should look like this:</p> <pre><code>from Bottle import get_url # ... @route('/') @view('index') def index(): return { 'get_url': get_url } </code></pre> <p>And in <code>index.tpl</code>, <code>.js</code> should be referenced as:</p> <pre><code>&lt;script type="text/javascript" src="{{ get_url('static', path='main.js') }}" charset="utf-8"&gt;&lt;/script&gt; </code></pre> <p><code>get_url</code> finds a handler with <code>name='static'</code>, and calculates the proper path to it. For dev server, this will always be <code>/static/</code>. You can probably even hard-code it in the template, but I don't recommend it for two reasons:</p> <ul> <li>You won't be able to mount your app anywhere but under root in production; i.e., when you upload it onto the porduction server, it can be placed under <a href="http://example.com/" rel="noreferrer">http://example.com/</a> (root), but not under <a href="http://example.com/myapp/" rel="noreferrer">http://example.com/myapp/</a>.</li> <li>If you change the <code>/static/</code> dir location, you'll have to search it all over your templates and modify it in every single template.</li> </ul>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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