kassold
kassold

Reputation: 469

Hyperlinks in web.py

How do I make web.py fetch a page when I click on a link? I have this in my template:

<a href='add.html'>Home</a>

When I click on 'Home', I get 'not found'.

In my application, I have '/add' mapped to the 'Add' class which returns 'Boom!' using the template add.html.

urls = ('/', 'Index',
    '/add','Add')

class Add(object):
    def GET(self):
        return render.add('Boom!')

I feel when I request for /add.html, the Add class will handle the request and return 'Boom!'

Why do I keep getting 'not found'?

Upvotes: 3

Views: 1474

Answers (2)

mit
mit

Reputation: 11261

The origin of a page URI ending in .html lilke /add.html is that in static hosting environments those were traditionally really single text files with html-content and the file ending for such a file is .html

But your system is dynamic and creates web pages on the fly. It does not necessarily need the pages ending in .html. Although you can mimic the traditional behaviour if you desire.

But in a modern and dynamic system it is often preferred that there is no ending on the single "pages" at all.

So you could, as Ignacio also suggests, just get rid of the .html and request the /add page, which is a valid and sufficient unique identifier for that resource.

If you like to keep the .html ending. you have to add it also in the route.

urls = (
  '/', 'Index',
  '/add.html','Add',
)

You can also have multiple routes pointing to the same resource, so that both /add and /add.html are valid and showing the same content, but content duplication has other drawbacks:

urls = (
  '/', 'Index',
  '/add','Add',
  '/add.html','Add',
)

I recommend getting rid of the .html. This means you stick with the code from your question and create links to the page like this:

<a href="/add">add something</a>

Upvotes: 8

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798744

Either change the route to be "/add.html" or change the link to be "add".

Upvotes: 5

Related Questions