Forums

Display of images and usage of 3D matplotlib

I basically managed to make my page run, but I have problems with displaying images and graphs.

  1. My static images won't show. In my html I use: img src={%static 'images/Capture.PNG'%} alt="" (with the arrow signs around, which don't show here) and I also tried it with img src=/static/images/Capture.PNG alt="" and both don't deliver the images. I checked and the folder and the files are there. How can I access them?
  2. I generate a 3D plot from the user input and want to output it as well. However, it results in an error saying: 'Unknown projection '3d'' in the line ax = fig.gca(projection='3d') despite that I include this in the code as well: from mpl_toolkits.mplot3d import Axes3D How can I make this work?

Both issues do not occur if I let the code run in the browser from my computer. In my settings.py file I have following code:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file))) TEMPLATES_DIR=os.path.join(BASE_DIR,'templates')

STATIC_DIR=os.path.join(BASE_DIR,'static')

STATIC_URL = '/static/' STATICFILES_DIRS=[ STATIC_DIR, ]

Thanks

two things to double check:

  1. did you run collectstatic on PythonAnywhere
  2. did you set up the static files mappings under the webapps tab?

Thanks. I ran collectstatic after including STATIC_ROOT = os.path.join(BASE_DIR, "static") in the settings.py. The response is this:

django.core.exceptions.ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting

the static related code in the settings.py file is now:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file))) TEMPLATES_DIR=os.path.join(BASE_DIR,'templates')

STATIC_DIR=os.path.join(BASE_DIR,'static')

STATIC_ROOT = os.path.join(BASE_DIR, "static")

STATIC_URL = '/static/'

STATICFILES_DIRS=[ STATIC_DIR, ]

STATIC_ROOT is where the files from STATICFILES_DIRS are put when you do collectstatic, so you can't have STATIC_ROOT in the STATICFILES_DIRS. Here is the explanation of STATIC_ROOT https://docs.djangoproject.com/en/1.10/ref/settings/ (search for STATIC_ROOT and the explanation of STATICFILES_DIRS is just below)

thanks. the link you provide oddly links back to this thread.

Huh. That's weird. I think it was the anchor that was breaking it. It's working now.

Okay, I think, I successfully ran collectstatic. That was the response:

63 static files copied to '/home/Equinox/homepage2/online_comp/static', 5 unmodified.

Then I followed the second advice by Conrad and I set up the files mapping under the web tap (basically following this guide: https://help.pythonanywhere.com/pages/DjangoStaticFiles/ )

But neither do my images show in my webpage nor can I access them through a direct link as suggested in the link above.

using 'inspect' in the browser, it tells me for the images that:

Failed to load resource: the server responded with a status of 404 (Not Found)

in my html file (that is in this folder: /home/Equinox/homepage2/online_comp/templates/bayes) (where 'homepage2' is the project from github and the virtual environment, 'online_comp' is the project name and 'bayes' ist app name) I have the following relevant code:

{% load staticfiles%}

< img src={%static 'images/update_rules.PNG'%} width=500 >

Further, that image is stored in this directory:

/home/Equinox/homepage2/online_comp/static/images

in my settings.py is this relevant code:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file))) TEMPLATES_DIR=os.path.join(BASE_DIR,'templates')

STATIC_DIR=os.path.join(BASE_DIR,'static')

STATIC_ROOT = os.path.join(BASE_DIR, "static")

STATIC_URL = '/static/'

STATICFILES_DIRS=[os.path.join(BASE_DIR, "staticfiles"),]

What do I have to do now to get this thing running?

Did you reload your webapp after adding the static files entry?

If by 'reload' you mean clicking the little reload button in my browser, yes. Or is any other reload function?

No, the big green reload button on the web tab?

okay, I did this now. And now nothing works anymore. Instead when calling my page the following error appears.

DisallowedHost at /

Invalid HTTP_HOST header: 'equinox.pythonanywhere.com'. You may need to add 'equinox.pythonanywhere.com' to ALLOWED_HOSTS.

Well, I did that and added 'equinox.pythonanywhere.com' to allowed hosts in settings.py (and saved). With no effect, there is still the same error message when loading the page.

Did you reload your web app after you changed settings.py?

okay, now I reloaded it with the green button again and the error disappeared.To make place for another error:

NoReverseMatch at /

Reverse for 'bayes_create_and_update' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

and the highlighted line in the Traceback is:

return render ( request, 'bayes/index.html')

Apparently it doesn't find the the correct template. No wonder, because the view it refers to ('bayes_create...') doesn't exist actually anymore. I have changed and split this view into 2 views with different names. The old code, however, is still there, commented out. But it still seems to read it. Even if I delete the unwanted code entirely (including the call of it), the same error pops up, referring to the same view, of which the server actually should not even be aware of, because it is entirely deleted from the code. I have encountered this phenomenon also before I pushed the green reload button. About 20% of the normal page reloading attempts (seemingly random) resulted in an error because it referred to code and files that either were commented out or were deleted (also the calls thereof) at some point before. How can that be?

I have erased everything and try everything anew.

that sounds like a combination of the error log updating say a minute later (ie. you are seeing old errors) and maybe the reload happening before the file changes have synced across. this rarely happens, but if you want to be safe, maybe wait a couple seconds etc before hitting reload.