1

I have a google verification file that I need to have in my root directory. How do I serve it and set up the route correctly in my app.py file? I thought just having it in the static directory would do the trick.

In my app.py file:

import requests; requests = requests.session() from flask import ( Flask, g, session, request, render_template, abort, json, jsonify, make_response ) from jinja2 import TemplateNotFound app = Flask(__name__) 

...

@app.route('/ping') def ping(): return "OK" """ Catch-All Route first looks for templates in /templates/pages then looks in /templates finally renders 404.html with 404 status """ @app.route('/', defaults={'path': 'index'}) @app.route('/<path:path>') def show_page(path): if session.get('tracking_url'): session['session_url'] = False templates = [t.format(path=path) for t in 'pages/{path}.html', '{path}.html'] g.path = path try: return render_template(templates, **site_variables(path)) except TemplateNotFound: return render_template('404.html', **site_variables(path)), 404 application = app if __name__ == '__main__': app.run('0.0.0.0', debug=True) 

I've tried adding this but it didn't work:

@app.route('/myfile.html') def myfile(): return send_from_directory('/static', 'myfile.html') 
6
  • You are only serving items from your template directory; if Google looks for the static file in the root then it won't find items in the /static route however. Commented Apr 29, 2014 at 14:38
  • you are effectively preventing any static files to be found. Commented Apr 29, 2014 at 14:51
  • @njzk2: Werkzeug is pretty good at sorting routes and /static/... will be checked before this catch-all route. Commented Apr 29, 2014 at 14:53
  • When I place the file in templates it doesn't find it. How can I have a route to myfile.html that's stored in the static directory work without changing the catch-all route? Commented Apr 29, 2014 at 14:57
  • @MartijnPieters: thanks for the precision. I would have though otherwise. (no way to prevent statics from being rendered, then?) Commented Apr 29, 2014 at 15:02

1 Answer 1

1

For a one-off file that Google looks for in the root, I'd just add a specific route:

 from flask import send_from_directory @app.route('/foobar_baz') def google_check(): return send_from_directory(app.static_folder, 'foobar_baz') 

You are free to add in test in show_page(path) to try and serve path with send_from_directory() before you test for a template, of course; the second filename argument can take relative paths.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.