I have problem with image in django. I get 404.
Settings.py
MEDIA_ROOT = 'static/image/'
MEDIA_URL = 'image/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
Models.py
img = models.ImageField(upload_to = '', verbose_name= 'Картинка',null=True )
(P.S I tried everything,but it isnt working)
Have you got your answer or not if not try this
Add this in your settings.py file
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') #new
MEDIA_URL = '/media/' #new
After Add in your main urls.py file add these config
from django.conf import settings #new
from django.conf.urls.static import static #new
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('App.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) #media
Related
I was trying to save an image but it can't be displayed through the browser. It was successfully saved in the exact location but when I pasted the link it says, "Page not found". Could someone help me with what's going on?
See output:
Browser View
Code:
Code Snippet
Settings configuration:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_LOCATION = "static"
MEDIA_LOCATION = "media"
settings.py
from pathlib import Path
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py
This must be applied in your settings configuration.
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
....
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I tried following this or this or this but nothing works for me - I have a directory called media which has uploaded files in it - but I cannot access it with http://127.0.0.1:8000/media/ - Page not found (404).
My settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'PROJECT/static'),
]
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL = "/media/"
and my urls.py:
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from django.views.generic.base import RedirectView,
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('home/', RedirectView.as_view(url='/')),
path('admin/', admin.site.urls),
path('', include('appname.urls')),
path('', include('django.contrib.auth.urls')),
path(r'^login', auth_views.LoginView.as_view(template_name='registration/login.html')),
]
if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Bonus question because I could not try it until now: How would I access an image with filename.jpg in /media/ in a template?
I fixed the issue with trial and error:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'projectname/static'),
]
MEDIA_ROOT = os.path.join(BASE_DIR, "projectname", "media")
MEDIA_URL = "/media/"
try it worked
your_app urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
....
]
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
STATIC_URL = "/static/"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static")
)
STATIC_ROOT = (
os.path.join(BASE_DIR, "staticfiles")
)
MEDIA_URL = "/media/"
MEDIA_ROOT = (
os.path.join(BASE_DIR, "media")
)
I have modified my Geonode project, which is a GeoDjango project, to enable multi-tenancy using django-tenants. I am currently not able to view my thumbnails due to broken routing...
How do I correctly route my generated thumbnails like this: http://d3.demo.com(current_tenant_domain_url)/uploaded/d3(tenant)/thumbs/document-8a72dc8c-0151-11eb-a488-1062e5032d68-thumb.png
The thubnail url that is currently generated is as follows: http://localhost:8000/uploaded/thumbs/document-fcdea3a4-015c-11eb-a488-1062e5032d68-thumb.png?v=c1855f6a
urls.py
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.LOCAL_MEDIA_URL,
document_root=settings.MEDIA_ROOT)
My current settings.py
MEDIA_ROOT = os.getenv('MEDIA_ROOT', os.path.join(PROJECT_ROOT, MEDIAFILES_LOCATION))
MEDIA_URL = os.getenv('MEDIA_URL', '%s/%s/%s/' % (FORCE_SCRIPT_NAME, MEDIAFILES_LOCATION, MULTITENANT_RELATIVE_MEDIA_ROOT))
Any help will be appreciated
settings.py
MIDDLEWARE = [
'django_tenants.middleware.main.TenantMainMiddleware'
]
DATABASE_ROUTERS = (
'django_tenants.routers.TenantSyncRouter',
)
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
MEDIA_URL = '/media/'
DEFAULT_FILE_STORAGE = 'django_tenants.files.storages.TenantFileSystemStorage'
MEDIA_ROOT = os.path.join(BASE_DIR, 'public', 'media')
urls.py
urlpatterns = [...] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I am try to add the static fills in python manage.py collectstatic but doesn't work
settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'yuvaraj/static/')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', jobs.views.home, name='home'),
path('blogs/', include('blogs.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have a problem with my static and media settings, so my uploaded images doesn't show up on my site page.
In my "settings.py" I have:
MEDIA_ROOT = (
os.path.join(BASE_DIR, '..', 'static', 'media')
)
MEDIA_URL = '/media/'
STATIC_ROOT = (
os.path.join(BASE_DIR, '..', 'static'),
)
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '..', 'static'),
)
In my "models.py" I have:
expert_photo = models.ImageField(upload_to='profiles')
Some "urls.py":
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
url(r'^experts/all/$', 'expert.views.experts', name='experts'),
url(r'^experts/get/(?P<expert_id>\d+)/$', 'expert.views.expert', name='expert'),
)
And after all of that, when I go to my page, I see, that the picture have link, like this:
http://127.0.0.1:8000/static/profiles/i_vagin.jpg
But it must be:
http://127.0.0.1:8000/static/media/profiles/i_vagin.jpg
So how can I fix that?
Media files are not, by default, served during development. To enable media file serving, you need to add the following code in your urls.py file:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Source: Django docs
Update
You'll also need to access images in your templates like this: {{ expert_photo.url }}