Forums

static files

[mod edit] This question is largely outdated since we released our static files support, which you'll find on the Web tab

For django-specific config, see this wiki page

cant use them through my app: whats the right path to them?

on localhost it works fine with paths like: 'rssDjApp/static/somefile.png'

here i tryed different paths, but there IOError like "No such file or directory: u'static/topResults.png'"

i have read tutorial and add urlPatterns in urls file

Hi kernie_xvid -- can I take a look at your code? And could you give me a specific path that should work, but doesn't? You can send me the information over the "Send feedback" link at the top of the page if there's stuff that you want to keep private.

on localhost (archlinux) im using django 1.4.1, project (rssDj) is located at: ~/workspace/rssDj/; there folders rssDj, rssDjApp, rssReader and file manage.py;

path from file rssDjApp/views.py in function:

drawFreqWordDiagram(wordInfos, "rssDjApp/static/topResults.png")

works correctly;

on yours server theres some modification on that file: in imports added "rssDj..." (from rssDj.rssDjApp.forms), in settings this two lines enabled:

# Uncomment these two lines to enable your static files on PythonAnywhere
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns();

in urls added "rssDj" also... (...'rssDj.rssDjApp.views.home', name='home')

others the same thing, but app's static paths isnt accessible

Hi kernie_xvid -- I can probably help you debug if you give me permission to look at the code in your account, but my guess is that what you're trying to do is read from or write to a static file from within a view rather than from a browser -- that is, you have a Django view that wants to load some data from a file, do something to it, and then return the result. Is that correct?

If so, then the problem is almost certainly that the working directory differs between the two environments. When you run your Django app locally, it will run in the directory where you run the runserver command from. On PythonAnywhere (and in most production hosting environments), the working directory will be different.

A good way to access files from your views is to have something defined in your settings.py that you can use to determine the location of your Django code, and then to use that as a root when accessing files. So, for example, in settings.py you would do this:

import os
SETTINGS_DIR = os.path.dirname(__file__)

Then in the view where you're trying to read the file, you'd do this:

drawFreqWordDiagram(wordInfos, os.path.join(settings.SETTINGS_DIR, "rssDjApp/static/topResults.png"))

...you'd also need this at the top of the views.py file:

import os
from django.conf import settings

It's worth mentioning that this is a different thing to Django's built-in "static files" support; the static files support exists to provide a simple way for requests from a browser to (say) http://someone.pythonanywhere.com/static/foo.png to load the file foo.png into the browser without having to write special views for every static file in your app. Of course, you might be trying to do that too...

thank you