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.
Related
In my localhost server, I was able to restrict users from accessing pdf media files which they are not supposed to access given that they are not the uploader of the file or the admin of the system.
The problem is that when I tried to deploy my application, the restriction on my media files no longer works.
This is my urls.py
urlpatterns = [ path('media/pdf/<str:path>', views.pdf, name='pdf'), ]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)
And my MEDIA_URL and MEDIA_ROOT in settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
Also my debug is still set to TRUE in deployment, could this also be the problem?
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.
This urls.py of base project
urlpatterns = urlpatterns + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
This is the settings.py
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')
This code is to add items to the database dynamically and i am not able to understand why is he adding the urlpatterns.
He is adding media urls to the url patterns. So for example if you had an image or video or something stored in your django project, you can use the browser to access these files at MEDIA_URL. In the settings.py, you are setting MEDIA_URL (where you can go in the browser '/media/'), to point to the contents of your MEDIA_ROOT (the 'media' folder)
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/
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.