how to run static files in pythonanywhere hosting? - django

I'm trying to upload my project via pythonanywhere but I always get failed to load static files I tried to download it by the static files section of web tab that exists into pythonanywhere and also I got failed you can see what I did here, I will show you all details that I did to help you to understand what could you give me the help through it:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# STATIC_ROOT = "/home/abdelhamedabdin96/website/website/static/index"
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# MAIN_DIR = os.path.dirname(os.path.dirname(__file__))
# STATICFILES_DIRS = (
# os.path.join(MAIN_DIR, 'static'),
# )
and in urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

It seems like you haven't run collect static command, You need to run this command
python manage.py collectstatic
Here its explanation for more info vist to the django official docs
django.contrib.staticfiles collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production.

Try use Aws console and its very cheap. I see bugs on Pythonanywhere. ie. Scheduler and a lot of limitations.

Related

All static files returning 404 error when deploying Django Project using Digital Ocean

I have tested my app in development and successfully got it fully functional in development running on my local server. I have now been trying to push it into production and none of the static files are being served. The connection has been successful as the domain shows the app with just plain HTML. However, all static files are returning the same 404 error.
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))`
STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
EDIT: installing whitenoise fixed this!
try this :
urls.py
from django.conf.urls.static import static
urlpatterns = [
...
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Or just try this command:
python manage.py collectstatic
try this on top every HTML file if you didn't:
{% load static %}
this is because when you add some static files then you must tell manually the template to load those static file.
for more you can see here

What is the correct settings for static files in django

I just started using django and I got confused over the static files. As per this post correct static files setting I understand that STATIC_URL is just like the name. STATICFILES_DIRS is the place where django will look for static files and STATIC_ROOT where the static files will be collected to. For my project I had the following file sys
rest
|__ rest
|__ settings.py
pages
static
|__admin
|__images
|__vendor
|__bootstrap
templates
manage.py
I decided to go for a project based approach when having my folders instead of a per app one. Some stuff was not working with the website landing page I had and I saw that I needed to collectstatic and so I did but I set the path to my already existing static file which did not let me at first but somehow ended up working. Out of nowhere my static folder had admin on it which I assume is from the admin app that comes with django, and my project finally started to work properly which is the confusing part. I decided to follow the post and included in my settings the following
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
Now I have a staticfiles folder along with my static folder. Also within the staticfiles folder I have everything I have in the static folder and that does not seem right and would like to know how to fix this. I am confused and a bit concerned that I will break everything again so any knowledge provided will be helpful.
I have used like this and its working fine:
STATIC_URL = '/static/'
STATICFILES_DIRS =[os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and use to implement code in the main Project urls.py and not in-app urls.py
urlpatterns += static(settings.STATIC_URL, documents_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, documents_root=settings.MEDIA_ROOT)

In production Django {{object.image.url}} not working in production. How to show image/MEDIA_ROOT/ in production?

When I set DEBUG=False in app/settings.py
{{object.img.url}} not working. How to fix this?
when I inspect it's img.url getting /images/image_name.jpg like this. In DEBUG=True http://127.0.0.1:8000/images/image_name.jpg it shows image. But when I set DEBUG=False this http://127.0.0.1:8000/images/image_name.jpg this didn't show anything.
my media root
MEDIA_URL = '/images/'
MEDIA_ROOT = (BASE_DIR / 'static/images')
in my URL I added
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
How to show image/MEDIA_ROOT/ in production.
As django doc says, it is:
Helper function to return a URL pattern for serving files in debug
mode:
See more here. So, this function returns no URL patterns if not in DEBUG mode.
In general, as a rule of thumb, Django should not serve static content, i.e. static or media files. Some other, like nginx, apache, etc. should serve static content in production environment.
Following 4 settings are important.
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = "https://xyz.abc.com/static/"
STATIC_ROOT = "/home/kcsl/web/xyz.abc.com/public_html/static/"
MEDIA_URL = "https://xyz.abc.com/media/"
MEDIA_ROOT = "/home/kcsl/web/xyz.abc.com/public_html/media/"
Use collectstatic command also
$ python manage.py collectstatic

Static file management in Django1.4

I've been confused with static files in Django for days. I found one solution that worked fine. But it collapsed when I set DEBUG=False. So I build up a new project and do some tests to get a clearer look.
First I create a project with the default settings. Then I changed some lines of the setting file into:
STATIC_ROOT = '%s/site_media' % PROJECT_DIR
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(STATIC_ROOT, 'images'),
)
After that, I put 'hi.jpg' at 'project_dir/images/hi.jpg'. I call runserver and visit 'http://127.0.0.1:8000/static/images/hi.jpg'. It doesn't work. What's the problem?
Here's how it works: when DEBUG=True then Django serves the static files itself. When DEBUG=False then Django won't do that anymore and you'll need to configure your web server to do it (such as Apache).
Django has a mechanism for that in django.contrib.staticfiles (see Managing static files and The staticfiles app). It basically means that you need to run the collectstaticmanagement command which will search for all static files in /static/ directories in your Django project and it will put them in one directory (STATIC_ROOT). When that has been done, your web server can serve the static files from that directory.
If one or more static files can't be found after running collectstatic then that means you have configured something incorrectly.
settings.py
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'site_media')
MEDIA_URL = '/site_media/'
STATIC_URL = '/static/'
if DEBUG:
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
else:
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static'),
)
urls.py
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
:)
Django: Migrating from MEDIA_URL to STATIC_URL

Serving static files for development mode in Django

I'm having trouble serving static files in development mode in Django. I do know that this is not a setting that should be used in a production server, so don't worry. For now however I'd like to stick to it.
The relevant parts of settings.py are:
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__) + '/..'), 'media')
STATIC_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__) + '/..'), 'static')
And of urls.py:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
So the static files are located for now in the directory named static right outside the project folder. I verified that STATIC_ROOT is evaluated to an appropriate value. I've double checked that the folder exists.
However when pointing my browser to the address localhost:8000/static/js/somefile.js, I get the dreaded 404 Page Not Found with the message 'js/somefile.js' could not be found.. Could you please suggest some reasons to this behaviour?
Thanks in advance.
EDIT:
I think I know where the problem may be: The thing is that in development mode Django attempts to look for the files from the STATIC_URL in the static/ subdirectories of all the installed apps. However I've added some additional files to my STATIC_ROOT and these are not served at all. Maybe there is some clash.
EDIT (2):
This must be it. When I run the server with ./manage.py runserver --nostatic it works, that is it actually serves the files from the STATIC_ROOT directory. What can I do about it? The problem is that just as I try to keep all my template files separate from the project itself I try to do the same with certain css and js files...
It doesn't work, because what I was trying to do wasn't very wise.
That's how it should be configured. settings.py:
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
MEDIA_ROOT = 'media.mydomain.com'
STATIC_ROOT = 'static.mydomain.com'
STATIC_DIRS = (
os.path.join(os.path.abspath(os.path.dirname(__file__) + '/..'), 'static'),
)
All the files remain in place, exactly where they were.
I had a similar problem, adding an explicit reference to the media location in my urlconf as per this fixed the problem for me.