how to navigate from one html to other in tornado using anchor t

ghz 10hours ago ⋅ 1 views

how to navigate from one html to other in tornado using anchor tag

Hi all here is a small application in tornado can any off u please tell me how to navigate from one html to other P.S Now i am getting a error 404 not found should i be using appengine or any other thing is required thanks in advance

enter code here
 import tornado.httpserver
 import tornado.ioloop
 import tornado.options
 import tornado.web


 from tornado.options import define, options

 define("port", default=8888, help="run on the given port", type=int)


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("template.html")
class indexhandler(tornado.web.RequestHandler):
    def get(self):
        self.render("index.html")



def main():
    tornado.options.parse_command_line()
    application = tornado.web.Application([
    ("/", MainHandler),
    ( "/index.html",indexhandler),
    ])
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()


if __name__ == "__main__":
main()

html page 1 template

<html>
<title>
basics
</title>
<head>
tornado
</head>
<body>
<h1> template</h1>
 <a href="index.html">goes to index.html </a> 
</body>
</html>

html -2 index.hmtl

enter code here
<html>
<head> index
</head>
<body>
<h2>2nd page in this application</h2>
</body>
</html>

Answer

It looks like you're on the right track with your Tornado application, but you're getting a 404 error when navigating between HTML pages. This is likely because the index.html page is not being correctly served by Tornado, and possibly because of a missing or misconfigured template path.

Here are a few suggestions to fix your issue:

1. Check Template Path

Tornado looks for templates in a specific directory. If you don't specify a directory for the templates, Tornado will look in the current working directory. Make sure that both template.html and index.html are in the correct directory (e.g., a folder named templates in the same directory as your Python file).

Modify your Application initialization to include the template path:

application = tornado.web.Application([
    ("/", MainHandler),
    ("/index.html", indexhandler),
], template_path="templates")  # Add this line to specify the template path

This will tell Tornado to look for templates in a directory called templates.

2. Ensure HTML Files Are in the Right Location

Make sure the HTML files (template.html and index.html) are located inside a directory called templates. Your project directory structure should look something like this:

my_project/
├── main.py  # Your Tornado app
└── templates/
    ├── template.html
    └── index.html

3. Check the URL Structure

In the first HTML page (template.html), you're linking to index.html with this line:

<a href="index.html">goes to index.html</a>

Tornado routes URLs, so the path index.html will be matched to your handler that serves the index.html page. Your current handler indexhandler is set up correctly to respond to /index.html.

If you don't want to use .html extensions in the URLs, you could modify your URL pattern to:

("/index", indexhandler)  # Use a more clean URL

And update the link in template.html to:

<a href="/index">goes to index</a>

4. Run the Application

Finally, make sure you're running your Tornado app correctly. You can run your Tornado app with the following command:

python main.py

This will start the server on port 8888 (or any port you specify). Visit http://localhost:8888/ in your browser to test the application.

Updated Code:

Here's your updated Tornado application, assuming the templates are in the templates directory:

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options

define("port", default=8888, help="run on the given port", type=int)

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("template.html")

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("index.html")

def main():
    tornado.options.parse_command_line()
    application = tornado.web.Application([
        ("/", MainHandler),
        ("/index", IndexHandler),  # Changed to /index
    ], template_path="templates")  # Added template path for Tornado to find HTML files
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

if __name__ == "__main__":
    main()

HTML Example 1 (template.html):

<html>
<title> Basics </title>
<head> Tornado </head>
<body>
<h1> Template </h1>
<a href="/index">Go to index</a>  <!-- Updated link -->
</body>
</html>

HTML Example 2 (index.html):

<html>
<head> Index </head>
<body>
<h2> 2nd Page in this Application </h2>
</body>
</html>

With these changes, the navigation should work correctly and the 404 error should be resolved. Let me know if you encounter any further issues!