Forums

Static Files are not being served

I've tried a few paths for the static files location configuration and the WSGI file containing folder in my config, but I cannot seem to get it right somehow. I'll describe the structure of my flask project below.

I have a file named app.py in markdown_blog/app/app.py. In this file I declared my routes and there is the magical line app = Flask(__name__).

In my wsgi file I have:

import sys
wsgi_path = '/home/Zelphir/markdown_blog/app/'
if wsgi_path not in sys.path:
    sys.path.append(wsgi_path)

from app import app as application

and only comments besides that.

My static files are inside my app under markdown_blog/app/static/. There are subdirectories there, for example markdown_blog/app/static/css or markdown_blog/app/static/js.

I pointed the static files path in the web config to: /home/Zelphir/markdown_blog/app/static/

The site itself runs, but as soon as I try to read any files from /static/markdown/chinese/blog/ inside my app.py, which contains markdown files, which contain blog posts, I get the following errors in the error log:

2016-05-15 15:28:17,891 :Exception on /chinese/blog [GET]
Traceback (most recent call last):
  File "/home/Zelphir/.virtualenvs/markdown_blog/lib/python3.5/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/Zelphir/.virtualenvs/markdown_blog/lib/python3.5/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/Zelphir/.virtualenvs/markdown_blog/lib/python3.5/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/Zelphir/.virtualenvs/markdown_blog/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/Zelphir/.virtualenvs/markdown_blog/lib/python3.5/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/Zelphir/.virtualenvs/markdown_blog/lib/python3.5/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/Zelphir/markdown_blog/app/app.py", line 25, in mandarin_blog
    complete_html_content = post_renderer.render_posts_from(dev_prefix + '/static/markdown/chinese/blog/')
  File "/home/Zelphir/markdown_blog/app/PostRenderer.py", line 12, in render_posts_from
    file_list = get_all_files_in_folder(directory_path, file_extensions=markdown_extensions)
  File "/home/Zelphir/markdown_blog/app/helpers/filesystem_helper.py", line 13, in get_all_files_in_folder
    for item in os.listdir(file_path):
FileNotFoundError: [Errno 2] No such file or directory: '/static/markdown/chinese/blog/'

So I get the impression that the mapping for static files does not work inside my app, maybe only works when a path to a static file is in the rendered HTML and then the browser asks for that file using the static link. However, I need to access those files before that in order to render the markdown files to blog post HTML. Also I need later access to CSS and JS files from the rendered HTML in the browser of the visitor.

How do I configure this?

My app's code can be seen on [Github].(https://github.com/ZelphirKaltstahl/MarkdownBlog)

The static files mapping is indeed a way to set up URL mappings so that external code (HTML, JavaScript, and so on) running on your users' browsers can access the files.

To access the static files from your code, just use a direct file path: /home/Zelphir/markdown_blog/app/static/markdown/chinese/blog/.

@giles:

Thanks for your quick response. Does the path need to be absolute? If it does I'd need to write some code so that it is different on my own machine for developing purposes - no big issue, but I'd like to have relative paths if possible.

Right now it does need to be absolute, but you can work around that by using the location of the current file to build up a path:

import os
...
current_file_directory = os.path.dirname(__file__)
file_path = os.path.join(current_file_directory, "../../something/file.txt")

Thanks! It's now working and your code snippet will probably proof useful in the future.

Excellent, glad it's working now.

Hi Giles, I have a question regarding Static HTML files...I've a folder that basically has a page for each candidate(their final reports) in HTML. I would like to display that from Django Admin as an Action. I'm having trouble accomplishing this. Mykl

What do you mean display as an action?

Sorry, I want the user to be able to select an Admin Action (drop down from the changelist form) which will display the report.

So you have already generated the report? In that case maybe the easiest is to go into your candidate models.py, add a function that gives you the link for that candidate, and put it into the admin.py list_display_links.

Hi, when I am hosting my website in pythonanywhere i got this error.Please help me to rectify this issue.![enter image description here][1]Thank you.I am adding the error.

< Pip installing django<2 (this may take a couple of minutes) >
   \
    ~<:>>>>>>>>>
Collecting django<2
  Downloading Django-1.11.10-py2.py3-none-any.whl (6.9MB)
    100% |████████████████████████████████| 7.0MB 94kB/s 
Collecting pytz (from django<2)
  Using cached pytz-2018.3-py2.py3-none-any.whl
Installing collected packages: pytz, django
Successfully installed django-1.11.10 pytz-2018.3
< Creating web app via API >
   \
    ~<:>>>>>>>>>
< Adding static files mappings for /static/ and /media/ >
   \
    ~<:>>>>>>>>>
Traceback (most recent call last):
  File "/home/bharathjinka09/.local/lib/python3.6/site-packages/pythonanywhere/django_project.py", line 52, in find_django_files
    self.settings_path = next(self.project_path.glob('**/settings.py'))
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/bharathjinka09/.local/bin/pa_autoconfigure_django.py", line 56, in <module>
    main(arguments['<git-repo-url>'], arguments['--domain'], arguments['--python'], nuke=arguments.get('--nuke'))
  File "/home/bharathjinka09/.local/bin/pa_autoconfigure_django.py", line 39, in main
    project.find_django_files()
  File "/home/bharathjinka09/.local/lib/python3.6/site-packages/pythonanywhere/django_project.py", line 54, in find_django_files
    raise SanityException('Could not find your settings.py')
pythonanywhere.exceptions.SanityException: Could not find your settings.py
16:44 ~ $

[edited by admin: formatting]

Hi bharathjinka09, are you following the django girls tutorial? In particular the deploy section of it?

It looks like the script could not find django's settings.py in your github repo. Are you sure you committed your code and pushed it up to github?