I have routers/handlers in Tornado app like
handlers = [
(r"/", BaseHandler),
(r"/salary/([a-zA-Z0-9]*)$", BaseHandler),
(r"/salary/([a-zA-Z0-9]*)/([a-zA-Z0-9]*)$", BaseHandler),
]
and static files inside settings like
static_path=os.path.join(os.path.dirname(__file__), "static"),
and when I call web page with url
/salary/99 it works fine
but when I call like
/salary/99/33 it doesn't work (doesn't work for any for router r"/salary/([a-zA-Z0-9])/([a-zA-Z0-9])$"). Doesn't work means doesn't load css and js => change static files path to /salary/static.... instead of /static...)
How adds this salary from router to static path, how to avoid this ?
What you want is instruct Tornado how to serve static files. Read the first paragraphs of http://www.tornadoweb.org/en/branch2.4/web.html#application-configuration, you will find that you will either define a tornado.web.StaticFileHandler or set static_path.
Related
So I have two domains:
MAIN_DOMAIN = www.example.com
DUTCH_DOMAIN = www.voorbeeld.com
I want to use the dutch domain with Flask-Babel for i18n support.
To make this possible I'm using host_matching like this:
def create_app(config_class=Config):
app = Flask(__name__, host_matching=True,
static_host=MAIN_DOMAIN)
Finally, routing:
#bp.route('/', host=MAIN_DOMAIN)
#bp.route('/', host=DUTCH_DOMAIN)
def index():
return render_template('index.html')
I might want to change the routing part in the future. But for now this works fine.
Now for the problem, when I navigate to the dutch domain everything works, except that I get many 404 error messages because it can't find the static files. If I change the url of the static files to the MAIN_DOMAIN it works fine. Which makes sense since static_host is set to MAIN_DOMAIN.
So I just can't seem to find a solution for this problem. Is there a way to make both domains for static files work? Or how would you solve this?
Ok. So I've figured it out and actually noticed some interesting behaviour.
So when I go to the main domain. url_for works as expected, so if you would do something like this in your templates:
<script src="{{ url_for('static', filename='jquery/dist/jquery.min.js') }}"></script>
The result would be this:
<script src="/static/jquery/dist/jquery.min.js"></script>
However, when I go to the DUTCH_DOMAIN route I get:
<script src="http://www.example.com/static/jquery/dist/jquery.min.js"></script>
So you don't have to manually add _external=True to url_for for you other domains but it all happens automagically. The reason it didn't work before is because of CORS since from the browser point of view you're loading assets from a different domain instead of from the "local" website.
Hope this helps.
I have a index.html file, which has the absolute path 'c:\project\web\frontend\index.html'
I am trying to return it using the following function
#webserver.route('/')
def home()
return webserver.send_static_file(path)
I have verified that the path is correct by accessing it directly in the browser.
I have tried to replace '\' with '/' without any luck.
It is running on a windows machine.
If you look at flask's documentation for send_static_file. You'll see that it says that it's used internally by flask framework to send a file to the browser. If you want to render an html, it's common to use render_template. You need to make sure that your index.html is in a folder called templates first.
So I would do the following:
#webserver.route('/')
def home()
return flask.render_template('index.html')
I had to define the path to be the static_folder, when creating the flask object. Once I defined the folder to be static, the html page was served.
I'm trying to write a web application and am using Tornado Web for the json xhr calls. But I'm trying to serve a static index.html which is to serve the main app.
How can I serve a simple page and still have requesthandlers for the rest of my application?
Here's what I tried so far:
import tornado.ioloop
import tornado.web
import json
import os
games = [...]
class HomeHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html')
class MatchHandler(tornado.web.RequestHandler):
def get(self):
self.write(json.dumps(games))
path = os.path.join(os.getcwd(), 'app')
if __name__ == "__main__":
application = tornado.web.Application(
[
(r'/', HomeHandler),
(r'/games', MatchHandler),
(r'/*.*', tornado.web.StaticFileHandler, {'path': path})
],
template_path=os.path.join(os.path.dirname(__file__), 'app')
)
application.listen(16001)
tornado.ioloop.IOLoop.current().start()
Thanks in advance!
The StaticFileHandler regex needs to A) contain a capturing group and B) use regex syntax instead of glob syntax:
(r'/(.*\..*)', tornado.web.StaticFileHandler, {'path': path})
This will match any paths containing a dot and send it to the StaticFileHandler.
Your code looks correct to me. Put a file named "index.html" in the "app" subdirectory of your current working directory when you run the app, and the contents of that "index.html" will be the response when you visit http://localhost:16001/
Your code should work fine, as #a-jesse-jiryu-davis answered. To expand a bit on it, you could use tornado.web.StaticFileHandler if you just need to serve your static file. This will make it more flexible, and also take advantage of server-side caching etc.
This question already has answers here:
python built in server not loading css
(2 answers)
Closed 7 months ago.
I have installed virtualenv in my localhost to run a django app with 1.8 version but when running it the css and js files doesn't load.
I get
Resource interpreted as Stylesheet but transferred with MIME type application/x-css
I have tried some options but they don't solve the issue either. I am running the same configuration on other PC and it works.
My HTML loads the css with :
<link href="/static/css/bootstrap.css" rel="stylesheet" type="text/css">
Adding following snippet into settings.py file may fix your problem:
import mimetypes
mimetypes.add_type("text/css", ".css", True)
This particular behaviour varies between the development (DEBUG=True) and deployment environment (DEBUG=False).
So if you are developing locally with DEBUG=False there is a high chance of this error. But once deployed on any server it will work without any error. If you want to avoid this error during development set DEBUG=True.
I ran into this issue during development (production was using Nginx and serving from /static_cdn folder without any issues).
The solution came from the Django docs: https://docs.djangoproject.com/en/3.1/howto/static-files/#serving-static-files-during-development
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
"whitenoise" will solve "MIME type" error then CSS is loaded successfully:
First, install "whitenoise":
pip install whitenoise
Then, set it to "MIDDLEWARE" in "settings.py". Finally, CSS is loaded successfully:
MIDDLEWARE = [
# ...
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware", # Here
# ...
]
open your Chrome by F12 Developer Tool and check what you actually received. In my case, the CSS file actually redirected to another page. so MIME is text/html not text/css
If you're using Centos and having similar issues (mine were with svgs) then you might need to install the mailcap package if it doesn't exist (as per this answer).
If you happen to be using the Django whitenoise plugin, then the mimetypes module is not used, and you need to pass in a dictionary of custom types in settings.py:
WHITENOISE_MIMETYPES = {
'.xsl': 'application/xml'
}
Access the CSS file directly. If you get a 404 error that is your answer, because Django serve you a text/html file when the browser expect text/css.
Try to fix your static files, and the problem is most likely solved.
In my case I only loaded
{% load static %}
in django and not added the
'{%static '<filename>'%}'
in hrefs when I added this the error's gone
Set DEBUG = TRUE, if you are in dev. You can later in production set it false.
I am using webpy framefork. I want to serve static file on one of requests. Is there special method in webpy framework or I just have to read and return that file?
If you are running the dev server (without apache):
Create a directory (also known as a folder) called static in the location of the script that runs the web.py server. Then place the static files you wish to serve in the static folder.
For example, the URL http://localhost/static/logo.png will send the image ./static/logo.png to the client.
Reference: http://webpy.org/cookbook/staticfiles
Update. If you really need to serve a static file on / you can simply use a redirect:
#!/usr/bin/env python
import web
urls = (
'/', 'index'
)
class index:
def GET(self):
# redirect to the static file ...
raise web.seeother('/static/index.html')
app = web.application(urls, globals())
if __name__ == "__main__": app.run()
I struggled with this for the last couple of hours... Yuck!
Found two solutions which are both working for me...
1 - in .htaccess add this line before the ModRewrite line:
RewriteCond %{REQUEST_URI} !^/static/.*
This will make sure that requests to the /static/ directory are NOT rewritten to go to your code.py script.
2 - in the code.py add a static handler and a url entry for each of several directories:
urls = (
'/' , 'index' ,
'/add', 'add' ,
'/(js|css|images)/(.*)', 'static',
'/one' , 'one'
)
class static:
def GET(self, media, file):
try:
f = open(media+'/'+file, 'r')
return f.read()
except:
return '' # you can send an 404 error here if you want
Note - I stole this from the web.py google group but can't find the dang post any more!
Either of these worked for me, both within the templates for web.py and for a direct call to a web-page that I put into "static"
I don't recommend serving static files with web.py. You'd better have apache or nginx configured for that.
The other answers did not work for me.
You can first load the html file in app.py or even write the html within app.py.
Then, you can make the index class' GET method return the static html.
index_html = '''<html>hello world!</html>'''
class index:
def GET(self):
return index_html