In my project I want to create custom view for 404 page
setting.py
DEBUG = False
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(os.path.join(BASE_DIR), "media_cdn")ode here
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)
handler404 = 'myapp.views.handler404'
handler500 = 'myapp.views.handler500'
When I make DEBUG = False in settings.py file, static files not loading properly. Screenshot attached for same.
static files not loading
Please help me to solve this issue.
When you set DEBUG = False your django development runserver will stop serving static files. You will have to setup a slightly different way to serve static files. Remember to remove those patterns for production though. On production setups static files should be served by the web servers such as Apache or Nginx.
If DEBUG = False, static() will return empty list. So you have to use any web server (apache, nginx, etc) to serve static files.Take a look at
https://docs.djangoproject.com/en/1.10/howto/static-files/deployment/
Related
When working locally on a Django project that uses static files and the default static files backend, I am running into an issue when I want to get full absolute urls to the static files instead of only the path.
My settings:
DEBUG = True
BASE_DIR = Path(__file__).resolve().parent.parent
INSTALLED_APPS = ["django.contrib.staticfiles", ...]
STATIC_ROOT = BASE_DIR / "static_root"
STATIC_URL = "/static/"
MEDIA_ROOT = BASE_DIR / "media_root"
MEDIA_URL = "/media/"
STATICFILES_DIRS = (BASE_DIR / "static",)
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
)
Everything works, the admin's CSS is loaded, images work, etc. But when I get the path of a file with this code:
from django.contrib.staticfiles.storage import staticfiles_storage
static_path = staticfiles_storage.url("path_to_folder/some_file.jpg")
That result is a relative path: /static/path_to_folder/some_file.jpg. This is a problem when these urls are used from my external frontend: it now has to prepend the backend's base url to all static assets. But if I'd want to deploy the static assets to S3 for example, in production, then I should not prepend these paths with the domain of the backend.
So what I tried to do was to change the STATIC_URL setting to http://localhost:8000/static/. That way the full url is passed to the frontend, but I could really easily change this setting in the backend in production.
And this is where the problem rears its head: when I change STATIC_URL to http://localhost:8000/static/, none of my static files work anymore, I just get a 404. The admin page also is completely unstyled because of this.
So, to make a long story short: how can I use an absolute STATIC_URL in local development mode with DEBUG=True?
I have created a reproduction repro: https://github.com/kevinrenskers/django-problem-repro.
Found the solution here: https://docs.djangoproject.com/en/4.1/ref/contrib/staticfiles/#django.contrib.staticfiles.views.serve.
Simply add this to the end of urls.py:
from django.conf import settings
from django.contrib.staticfiles import views
from django.urls import re_path
if settings.DEBUG:
urlpatterns += [
re_path(r'^static/(?P<path>.*)$', views.serve),
]
Then the static files will work again even with an absolute URL as the STATIC_URL setting.
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
I want to access logs easily on my app so I created a page for that, I click on a button and it download the log.
But I get a 404 error when I try to do that, I've set static files and media files
here are my settings
STATIC_URL = 'static/'
STATICFILES_DIRS = [
"frontend/static",
]
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media')
LOG_URL = 'static/logs/'
LOG_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static/logs')
and here are my urls
urlpatterns += static(settings.LOG_URL, document_root = settings.LOG_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
It works when I access /media/path/to/media.file, but not for /static/logs/path/to/logs.txt, it's the same configuration for both urls so I know I miss a point, but how could I fix it? Is it because I used static and not media? Thx a lot
It depends how you deployed your Django application?
Better add media/log folder path while serving application on the web.
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
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.