Not able to access stylesheet and javascript files from Web Py controller

人走茶凉 提交于 2021-01-29 09:24:09

问题


I'm very new to Python coding. I started building a simple site using Web.py. While I'm able to create all the simple routes like Home, Profile, etc properly, I'm facing an issue in setting up routes with regex characters. So, I have a route to the settings page on click of a link:

'/settings/(.*)', 'Settings'

In the * I will be passing the username value from the session object. Now on click, although the application routes properly to the Settings page, it's unable to load the appropriate styles and JS files from the static directory. Please find my folder structure below:

Project Folder Structure:

Project Folder Structure

Please find my controller file which I have configured:

import web
from Models import Users, Login, Posts

web.config.debug = False
urls = (
    '/', 'Home',
    '/settings/(.*)', 'Settings'
)

app = web.application(urls, globals())
session = web.session.Session(app, web.session.DiskStore("sessions"), initializer= {"user": None})
session_data = session._initializer

render = web.template.render("Views/Templates", base="CodeWizardContainer",
                         globals={'session': session_data, 'current_user': 
session_data["user"]})


class Home:
   def GET(self):
       return render.Home()


class Settings:
    def GET(self, user):
        return render.Settings()


if __name__ == "__main__":
     app.run()

All my CSS and JS files are under the static folder in ROOT directory. For other simple URL the styles are getting loaded file. However, for the setting dynamic URL the application is trying to load the styles and javascript again from /settings/static/cs or /settings/static/js folder, which is clearly not the correct path. But I didn't refer such paths for CSS or JS files anywhere in the application. Only during the setting route access, I'm getting such an issue. Please find the whole Python console log below:

127.0.0.1:57740 - - [23/May/2020 17:41:04] "HTTP/1.1 GET /" - 200 OK
127.0.0.1:57741 - - [23/May/2020 17:41:04] "HTTP/1.1 GET /static/js/scripty.js" - 304 Not Modified
127.0.0.1:57740 - - [23/May/2020 17:41:04] "HTTP/1.1 GET /static/css/mystyle.css" - 304 Not Modified
127.0.0.1:57748 - - [23/May/2020 17:41:19] "HTTP/1.1 GET /register" - 200 OK
127.0.0.1:57748 - - [23/May/2020 17:41:24] "HTTP/1.1 GET /settings/user1" - 200 OK
127.0.0.1:57749 - - [23/May/2020 17:41:24] "HTTP/1.1 GET /settings/static/css/mystyle.css" - 200 OK
127.0.0.1:57751 - - [23/May/2020 17:41:24] "HTTP/1.1 GET /settings/static/js/jquery-3.4.1.min.js" - 200 OK
127.0.0.1:57753 - - [23/May/2020 17:41:24] "HTTP/1.1 GET /settings/static/js/popper.min.js" - 200 OK
127.0.0.1:57748 - - [23/May/2020 17:41:24] "HTTP/1.1 GET /settings/static/css/bootstrap.css" - 200 OK
127.0.0.1:57755 - - [23/May/2020 17:41:24] "HTTP/1.1 GET /settings/static/js/bootstrap.min.js" - 200 OK
127.0.0.1:57757 - - [23/May/2020 17:41:24] "HTTP/1.1 GET /settings/static/js/scripty.js" - 200 OK

Although, the status is showing as 200 OK in browser the style and JS are not getting loaded for obvious reasons as the path /settings/static/css/ or /settings/static/js/ is not correct.


回答1:


You misunderstand a bit of what is going on here.

/settings/static/css/mystyle.css and friends are getting loaded successfully: they're just not what you think they are.

You don't list the contents of your Settings.html template file, so I can only guess, but the request for /settings/static/css/mystyle.css is resolved by the '/settings/(.*)' path, which becomes Settings.GET(user="static/css/mystyle.css") which returns render.Settings() again.

Plus, note that your render value is set once at program startup, so it's not going to change if/when you change your user value (at least that's what I think you're trying to do.) Instead, within your GET() you can configure for the selected user, and send possibly different values in the call to render.Settings()



来源:https://stackoverflow.com/questions/61971870/not-able-to-access-stylesheet-and-javascript-files-from-web-py-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!