Forums

static files are not loading on webiste

my webiste uses virtual environment and is loading on https://rhythm.pythonanywhere.com/ but static and media files are not loading. i have followed https://help.pythonanywhere.com/pages/DjangoStaticFiles/ instructions properly still am not able to figure out the problem.

settings.py

STATIC_ROOT = "/home/rhythm/portfolio_project/static"
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, '/home/rhythm/portfolio_project/portfolio')]
MEDIA_ROOT = "/home/rhythm/portfolio_project/media"                                                                               
MEDIA_URL = '/media/'

ran collectstatic afterwards

web tab settings for static and media: URL Directory
static/ /home/rhythm/portfolio_project/static
media/ /home/rhythm/portfolio_project/media

reloaded the site as well but still not working. Awaiting for your kind response!

static files are not loading

It looks like you're missing a slash from the start of the URLs on your static files table -- they should be /static/ and /media/. If you correct that, then reload your website using the button at the top of the "Web" page, it should all work.

Such a silly mistake.Thank you so much for the help.

No problem, glad to help!

Thank you i also had the same issue

OK -- is it all working now?

Good evening,

I have the same problem as this person : my static files are not loading.

You can count on me when I tell you I've tried EVERYTHING : no silly mistakes, nothing is wrong in development env.

I've followed the nice and clean tuto from Pretty Printed

I've read every single articles written on pythonanywhere.com about this problem, even Django documentation. I've scrapped the whole web for that. Yet it doesn't work.

I've even tried to randomly collectstatic from several places within the code, doesn't work either.

I can even explain what to do to make it works :

in settings.py, one keep the default line STATIC_URL = '/static/', but in order to get files the server need to run collectstatic - others methods are possible, but I'm not a "web developper".

In order to make collectstatic command works, one need to add STATIC_ROOT and indicate where to provide static files for the server. Those static files should be at the root of the project like so :

STATIC_ROOT = '/home/user/project_name/static'

Or even better :

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

because os.path.join(BASE_DIR) value is basically the root of your project (eg : /home/user/project_name/)

HOW THE HECK I cannot make things work even though I know perfectly the process ?!

Here are the lines about static files on settings.py

STATIC_URL = '/static/'

//STATIC_ROOT = '/home/Sajtis/GardenApp' STATIC_ROOT = os.path.join(BASE_DIR, 'static')

sorry- I'm confused what works for you vs what doesn't work for you right now. (and when you say something works- what does that mean?)

It was pretty straightforward.

What didn't work ? My static files was not loading.

Then I briefly described what I tried, without changing the outcome.

Finally, I emphased on the fact that I was able to perfectly explain how to make it work even though I was not able to find what was wrong with my configuration.

Am I talking to the past ? Yep ! I've finally solved it.

What went wrong ? I'm not a web developper. Thus a sentence that is pretty clear for a developper was not clear for me.

Let's take a look at this - I admit it - nice tuto https://help.pythonanywhere.com/pages/DjangoStaticFiles/

Most of you guys are simply describing how to define static path like so :

"The important thing is this needs to be the full, absolute path to your static files folder."

If you have formulated the same information in a different way, I wouldn't have had any problem. This is how I found what was wrong. I said to myself the following :

"On the static file mapping setting, indicate the absolute path until you reach the ultimate directory before getting to your css, js or bs files."

Once I acted like above, it worked !

Glad to hear that you made it work!

i have a similar problem except that when I when i switch to developers tools and click on source, I see css codes in the js file under static which is odd. i specified both javascript and css path in the web tab but still not working

can you explain what you mean by seeing css code in js files?

I am new to pythonanywhere, python, and flask but have been scripting for years, some in html, and trying to get an html template setup within a flask frame to begin the process of building out a webapp. The point I am stuck at is loading a style sheet and an image from the index.html file. Below is my file structure. I have read this section from your “Static Files Mapping” page over many times and cannot figure out how to implement what you are saying I need to do.

“Static files config is basically a mapping from a URL to a directory. You tell us where your files are (eg /home/me/myapp/static), and what URL you want them served at (eg /static/) and then hit "Reload web app". Then, any files inside the static folder will be served, eg /home/me/myapp/static/css/base.css will appear at http://me.pythonanywhere.com/static/css/base.css. “

Here is the code for the index.html file. I need the correct coding to load the style.css and cycle.jpg file. Thanks. <!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" link href="/home/rlohmeyer/mysite/static/css/style.css"> </head> <body> <h1>Welcome to Lifecycles.com--Under Development</h1> <img src="/home/rlohmeyer/mysite/static/images/cycle.jpg"> </body> </html>

File Structure /home/rlohmeyer/mysite └── templates ├── index.html └── assets ├── css │ ├── style.css ├── images │ ├── cycle.jpg └── js

How does your mapping look like and what is the url?

The last time when Deployed my website it was working fine and able to load static, after a few days yesterday when I check It lost connection from the static CSS folder. Below is my Settings.py file connection code also I have set up my Static files in web sections as well still I am not able to get CSS. I have also tried to enable and disable HTTPS Security.

    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR,'static')
    STATICFILES_DIRS = (os.path.join(BASE_DIR,'static'),)

    DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR,'media')

Did you follow https://help.pythonanywhere.com/pages/DjangoStaticFiles ?