Question

I am experimenting in Bottle/MongoDB extending the Blog Engine from one of the MongoDB University Courses.

I have added a header & footer template and added some simple JS, CSS and an image. These work fine on all the templates other than show_post view. The way that it seems to be set up is that the value passed in to the function includes the static files (css, js & png) as well as the URL variable for the blog post itself.

The values from the python console are:

  • about to query on permalink = TLxrBfyxTZjqOKqxgnUP
  • 1.0.0.127.in-addr.arpa - - [25/Feb/2014 15:08:09] "GET /post/TLxrBfyxTZjqOKqxgnUP HTTP/1.1" 200 37682
  • about to query on permalink = style.css
  • 1.0.0.127.in-addr.arpa - - [25/Feb/2014 15:08:09] "GET /post/style.css HTTP/1.1" 303 0
  • about to query on permalink = blog.js
  • 1.0.0.127.in-addr.arpa - - [25/Feb/2014 15:08:09] "GET /post/blog.js HTTP/1.1" 303 0
  • about to query on permalink = BL_logo.png
  • 1.0.0.127.in-addr.arpa - - [25/Feb/2014 15:08:09] "GET /post/BL_logo.png HTTP/1.1" 303 0
  • 1.0.0.127.in-addr.arpa - - [25/Feb/2014 15:08:09] "GET /post_not_found HTTP/1.1" 200 21
  • 1.0.0.127.in-addr.arpa - - [25/Feb/2014 15:08:09] "GET /post_not_found HTTP/1.1" 200 21
  • 1.0.0.127.in-addr.arpa - - [25/Feb/2014 15:08:09] "GET /post_not_found HTTP/1.1" 200 21

The code in question is:

# Displays a particular blog post
@bottle.get("/post/<permalink>")
def show_post(permalink="notfound"):

cookie = bottle.request.get_cookie("session")

username = sessions.get_username(cookie)
permalink = cgi.escape(permalink)

print "about to query on permalink = ", permalink
post = posts.get_post_by_permalink(permalink)

if post is None:
    bottle.redirect("/post_not_found")

# init comment form fields for additional comment
comment = {'name': "", 'body': "", 'email': ""}

return bottle.template("entry_template", dict(post=post, username=username, errors="", comment=comment))

How can I prevent the function being called at all if the 'permalink' is a file rather than a value from a query string?

Thanks, Mark

ANSWER

With many thanks to Ron's answer below for pointing me in the right direction.

My error was down to the path to the static files being incorrect. I made the path dynamic by importing os and then changed the root value to os.path.join(os.getcwd(), 'static/js') and made the filepaths in my header.tpl absolute, e.g."/style.css".

@bottle.get('/<filename:re:.*\.css>')
def stylesheets(filename):
    rootPath=os.path.join(os.getcwd(), 'static/css')
    return bottle.static_file(filename, root=rootPath)
Was it helpful?

Solution

Because you've placed your assets under /post, they're conflicting with your /post/<permalink> route.

Typically, you would serve your assets (css, js, png) from their own directory; something like /static/css/foo.css. That's what I'd recommend here.

Something along the lines of this:

@bottle.get("/post/<permalink>")
    def show_post(permalink="notfound"):
        # code to generate a post page goes here

@bottle.get("/static/<file_name:path>"):
    def static_file(file_name):
        return bottle.static_file(file_name, "/full/path/to/your/static/file/root/")

The relevant Bottle docs are here; or see one of the many posts describing how to serve static file from Bottle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top