0

i created a robots.txt , i've looking for how to include it in mywebsite.com/robots.txt and i found Flask

This is my code

from flask import Flask, request, send_from_directory
app = Flask('maksc', static_folder='/static')


@app.route('/robots.txt')
def static_from_root():
return send_from_directory(app.static_folder, request.path[1:])

Everything ok , but when i go to mywebsite.com/robots.txt it cant found the page , it triggers python "Page not found" displaying all the urls defined

  • Please look here http://stackoverflow.com/questions/14048779/with-flask-how-can-i-serve-robots-txt-and-sitemap-xml-as-static-files and/or here http://stackoverflow.com/questions/4239825/static-files-in-flask-robot-txt-sitemap-xml-mod-wsgi and/or here https://vilimpoc.org/blog/2012/11/21/serving-static-files-from-root-and-not-static-using-flask/ – JOSEFtw Jul 30 '14 at 19:36
  • if it's displaying the url defined in the error page, then that's probably django's server – jibreel Jul 30 '14 at 20:24
  • It is django's server error page –  Jul 30 '14 at 20:25
  • Why you use `'maksc'` instead `__name__`? By `__name__` you will resolve module, by module you will resolve `root_path`, by `root_path` you will resolve static folder root (`os.path.join(root_path, static_folder)`). – tbicr Jul 31 '14 at 06:23

2 Answers2

0

Try setting static_url_path

app = Flask(__name__, static_url_path='/')

This means that files will be served from / instead of /static/blabla/

EDIT: Based on your comment it looks like you're using Django. Try adding this to your urls.py:

from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
...
    (r'^robots\.txt$', direct_to_template,
     {'template': 'robots.txt', 'mimetype': 'text/plain'}),
)

Example taken from here: http://fredericiana.com/2010/06/09/three-ways-to-add-a-robots-txt-to-your-django-project/

JOSEFtw
  • 9,781
  • 9
  • 49
  • 67
  • Didnt work either , i am thinking , i have to create an url on urls.py ? Because it doesnt seems to find anything not even the url –  Jul 30 '14 at 20:10
  • Im no Flask expert but I can't find anything about an urls.py here? Isn't that Django? http://flask.pocoo.org/docs/quickstart/ – JOSEFtw Jul 30 '14 at 20:18
  • Yes i am using Django , u cant combine them ? –  Jul 30 '14 at 20:26
  • Ehm...I don't think that's a normal use case at all? Aren't they two different frameworks? I just tried this simple Flask app, and it worked for me: `from flask import Flask app = Flask(__name__) @app.route('/robots.txt') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()` – JOSEFtw Jul 30 '14 at 20:28
  • I am such a noob haha , i did a little research , you cant use both , i will import robots.txt from django , thz for the help anyway –  Jul 30 '14 at 20:31
  • I found this, sure you CAN do it, but it's not recommended... Feel free to accept my "answer". http://tofudonuts.blogspot.se/2010/09/combining-flask-and-django.html – JOSEFtw Jul 30 '14 at 20:32
0

Try setting the static_folder='static' without the slash, as it may interpret it as a top root diretory.

jibreel
  • 359
  • 2
  • 8
  • Tried , doesnt work , as i said above , shouldn't i have to create an url on ulrs.py ? –  Jul 30 '14 at 20:15
  • no, you don't need to define url.py, how are you running the application server? and is the folder the app resides in called maksc ? – jibreel Jul 30 '14 at 20:21
  • I am running it on console if that's what you mean and yes its called like that –  Jul 30 '14 at 20:26