如何配置CherryPy从index目录及静态目录提供静态文件并支持自定义路径
Got it, let's get your CherryPy setup working exactly how you want it. Here's a tailored solution for your project structure:
Project Structure Recap
First, just to align on what we're working with:
project/ index/ about.html index.html forum.html profile.html settings.html apple-touch-icon.png static/ main.css forum.css main.js forum.js load-image.min.js server.py metaclass.py mailing.py errors.log
CherryPy Configuration & Code
Update your server.py with this code to map clean URLs and serve all your static content properly:
import cherrypy class Root: # No custom methods needed here—we're using CherryPy's built-in static file handlers pass if __name__ == '__main__': # Core configuration for URL mapping and static file serving app_config = { # Map root URL (/) directly to index/index.html '/': { 'tools.staticdir.root': './', 'tools.staticfile.on': True, 'tools.staticfile.filename': 'index/index.html' }, # Map clean, short URLs to their corresponding HTML files '/about': { 'tools.staticfile.on': True, 'tools.staticfile.filename': 'index/about.html' }, '/forum': { 'tools.staticfile.on': True, 'tools.staticfile.filename': 'index/forum.html' }, '/profile': { 'tools.staticfile.on': True, 'tools.staticfile.filename': 'index/profile.html' }, '/settings': { 'tools.staticfile.on': True, 'tools.staticfile.filename': 'index/settings.html' }, # Serve all other files in index/ (like apple-touch-icon.png) via /index/... '/index': { 'tools.staticdir.on': True, 'tools.staticdir.dir': 'index' }, # Serve your CSS/JS assets from the static/ directory '/static': { 'tools.staticdir.on': True, 'tools.staticdir.dir': 'static' } } # Start the CherryPy server with our configuration cherrypy.quickstart(Root(), '/', app_config)
How This Works
Let's break down the key parts:
- Clean URL Mapping: Each entry like
/aboutuses CherryPy'sstaticfiletool to directly link the short URL to its corresponding HTML file in theindex/folder. Users won't need to type/index/about.htmlanymore. - Root URL Handling: The
/configuration ensures visiting your site's root loadsindex/index.htmlautomatically. - Remaining Index Files: The
/indexsection makes sure other assets in theindex/directory (likeapple-touch-icon.png) are still accessible via their original paths (e.g.,yoursite.com/index/apple-touch-icon.png). - Static Assets: The
/staticsection serves your CSS and JS files exactly as you'd expect (e.g.,yoursite.com/static/main.css).
Optional: Make All Index Files Accessible From Root
If you want to hide the /index path entirely (so apple-touch-icon.png is accessible at yoursite.com/apple-touch-icon.png instead), adjust the root configuration like this:
'/': { 'tools.staticdir.root': './', 'tools.staticdir.on': True, 'tools.staticdir.dir': 'index', # Ensure root URL loads index.html by default 'tools.staticfile.index': 'index.html' }, # Keep the clean URL mappings for /about, /forum, etc. '/about': { 'tools.staticfile.on': True, 'tools.staticfile.filename': 'index/about.html' }, # ... rest of the config stays the same
This way, every file in index/ is available directly from the root, while your clean URLs still take priority for the main pages.
Just run python server.py from your project/ directory, and you're good to go!
内容的提问来源于stack exchange,提问作者Robert Moore

