Django Media Directory HTTP 404 for Uploaded Images - django

My project structure is:
- api/
- urls.py
...
- avatars/
- 16701016.jpg
- 16701019.jpg
...
- frontend/
- static/
- frontend/
- templates/
- urls.py
...
- website/
- settings.py
- urls.py
...
Part of the settings.py file:
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.abspath('../avatars/')
MEDIA_URL = '/avatars/'
The contents of /website/urls.py :
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('api.urls')),
re_path(r'^.*', include('frontend.urls'))
]
Now I need to access the /avatars/*.jpg files via URL like http://127.0.0.1:8000/avatars/*.jpg
But It is not working (Just 404). What is the problem?

Change the MEDIA_ROOT value from:
MEDIA_ROOT = os.path.abspath('../avatars/')
to:
MEDIA_ROOT = os.path.abspath('avatars')
And also append the following code in urls.py:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Related

Django doesn't load images from media

I've set things for the setting and the URLs as follow but it doesn't load the images from media directory:
settings.py:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '/media/')
urls.py:
urlpatterns = [
path('create/', views.image_create, name='create'),
path('detail/<int:id>/<slug:slug>/', views.image_detail, name='detail'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
this is the link generated for the page: https://127.0.0.1:8000/images/detail/1/django-and-duke/
and the link for the image: https://127.0.0.1:8000/media/images/2020/08/02/django-and-duke.jpg
I have another app named accounts and if I add the base URL of account to the first of this media URLs it works! but I know they are separated from each other.
if you need other parts of code please tell me.
Remove the slashes in os.path.join(BASE_DIR, '/media/')
If any argument to os.path.join begins with a slash / it will overwrite all previous arguments. You're currently setting MEDIA_ROOT to be /media/
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

images arent displaying from db in django 2.2 even if i get my url correct and include static and media root in settings and url

my image isnt displaying .
I have tried putting all the urls and roots for static and media but it isnt working for 2.2 version.
#in settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
#in main urls.py
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
#in app 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)

Can't import my own css file in TINYMCE_DEFAULT_CONFIG = { 'content_css':

As I said in the title I can't import my css file in TINYMCE_DEFAULT_CONFIG variable 'content_css'.
I'm using django-tinymce4-lite package and setting 'content_css' in my settings.py file
I've tried with the boostrap cdn like this:
'content_css': 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'
and it worked but if I do something like this:
"content_css": os.path.join(STATIC_URL, "css/style.css")
I have the following 404 error:
"GET /static/css/style.css HTTP/1.1" 404 1764
my css file is in a static folder located in the root directory of my project like this:
/static/css/style.css
and my Static conf is:
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/")
urls.py
urlpatterns = [
path("grappelli/", include("grappelli.urls")),
path("admin/", admin.site.urls),
path("admin/filebrowser/", site.urls),
path("tinymce/", include("tinymce.urls")),
path("page/", include("pages.urls")),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
It's been a whole day looking for a solution, even the smallest clue is welcome.
Thanks
EDIT:
I'm in DEBUG mode running 'python manage.py runserver'
Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
django = "*"
black = "*"
psycopg2-binary = "*"
pillow = "*"
django-tinymce4-lite = "*"
django-filebrowser-no-grappelli = "*"
[requires]
python_version = "3.6"
Add STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"), ) to your settings.
Also remove STATIC_ROOT for your development settings or change it to something that's outside the project directory, for example one level higher:
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '..', 'static'))
This will make django collect static files to a static directory next to your project directory (but it could be a different location altogether, where your webserver will fetch them directly when you don't run runserver)
Note I wrote a more thorough explanation of the various settings, especially when deploying to production here on SO and here in a blog post

Error trying to serve user uploaded files locally

I'm trying to serve user-uploaded images locally on Django 1.10. I'm following the documentation here and getting this error:
SystemCheckError: System check identified some issues:
Your URL pattern [<RegexURLPattern None ^media\/(?P<path>.*)$>] is invalid.
Ensure that urlpatterns is a list of url() instances.
The issue is from adding the static portion of my urls:
urlpatterns = [
...my urls...
]
if settings.DEBUG:
# This is causing the error.
urlpatterns += [
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
]
When I remove the static addition to my urls, the error goes away. What am I doing wrong here?
My applicable settings are as follows:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = BASE_DIR + "/static/"
STATIC_URL = "/static/"
MEDIA_ROOT = BASE_DIR + "/media/"
MEDIA_URL = "/media/"
The answer is that static should not be inside a list. This line:
urlpatterns += [
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
]
should be:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

How to manage media files on Windows?

I want to get media files for Django when developing application. But they just don't set up. This is my settings:
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR
STATICFILES_DIRS = (
STATIC_ROOT + '\\projectpackage\\static\\',
)
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR + '\\projectpackage\\media\\'
Templatetag:
<img class="img-responsive" src="{{ project.image.url }}" alt="">
Urls:
urlpatterns = [
url(r'^profile/', include(profile.urls)),
url(r'', include(authentication.urls)),
url(r'^project/', include(project.urls)),
url(r'^admin/', include(admin.site.urls)),
url(r'^', 'views.index', name='index'),
url(r'^markdown/', include('django_bootstrap_markdown.urls')),
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(STATIC_URL, document_root=STATIC_ROOT)
urlpatterns += static(MEDIA_URL, document_root=MEDIA_ROOT)
I can't see my mistake, because I've tried every other advices in other questions. And everything works bizzare. Static files are running good, media uploading to media/prj_img, but when I try to show image at template I've got such strange result:
<img class="img-responsive" src="/media/C%3A/Development/projectdirectory/projectpackage/media/prj_img/wallhaven-131_od9FWLX.jpg" alt="">
How could I fix this media error? This is strange, because everything looks right. Why there are full path in url?
Edit:
BASE_DIR
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
Also I've figured out that I've got wrong upload_to and changed it to prj_img. Now I've got following link:
<img class="img-responsive" src="/media/prj_img/wallhaven-24700.jpg" alt="">
But still it's not displating.
I have a lot of comments on your code.
STATIC_ROOT location is not appropriate. With your settings collectstatic command will put everything in your project's directory. Change it like so:
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
But take care, that this folder is accessable (both permissions and django-settings) in production.
Use os.path.join to join folders:
STATICFILES_DIRS = (
os.path.join(STATIC_ROOT, 'projectpackage', 'static')
)
This url pattern must be the last one and contain $:
url(r'^$', views.index, name='index'), # do not pass strings
These two url patterns do the same (docs):
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(STATIC_URL, document_root=STATIC_ROOT)
Moreover, you do not need them. Just ensure, that INSTALLED_APPS setting contains 'django.contrib.staticfiles'.