Forums

Configuring static files from PA for web2py app

Hi,

I am new to PythonAnywhere, I could see one option for serving static files directly using PA. I am not able to make out the path/url that needs to be configured there.

1 :- Since my complete app is using js/css files, then what path should I specify there such that these files load faster for the complete app.

2 :- I also have a page where I display the product images from database (images are uploaded in database - resides in uploads directory). Can I define it in static file settings as well, if yes, what should be the path ?

Maybe an example would help?

Supposing you have some static files stored like this:

/home/myusername/myproject
└── assets
    ├── css
    │   ├── base.css
    │   └── bootstrap.css
    ├── images
    │   ├── img1.jpg
    │   └── img2.png
    └── js
        └── widgets.js

And, supposing in one of your templates, you want to use some of your static assets, like this:

<link rel="stylesheet" link href="/static/css/base.css">
<script type="text/javascript" src="/static/js/widgets.js"></script>
[...]
<img src="/static/images/img1.jpg">

The two things to note are:

  1. The URLs all start with /static/
  2. The path to the place where all your static files are stored is /home/myusername/myproject/assets

So, what you want is a static files mapping that says:

The URL /static/ should map to the folder /home/myusername/myproject/assets

Does that makes sense? Can you see how, after that, the relative paths will work?

So now you know what to put in the two boxes on the Static files section in the web tab. Remember to hit "reload" when you're done. To test it, visit the URL of one of your static files directly. Remember to use Ctrl+F5 to force a reload of the page.