Static images are showing properly. The files in the media folder are not displaying in html. I tried setting up media_url in various ways but in vain. I uploaded the image via django admin panel. The name of product is showing fine. The img.url shows /media/p2.jpg
models.py
class Product(models.Model):
name = CharField(("Name"),max_length=256,blank=False)
title_img = models.ImageField(null=True, blank=True)
settings.py
PROJECT_ROOT = (os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(PROJECT_ROOT), "media_root")
urls.py
urlpatterns = [
path('', ProductList.as_view() , name="product"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
template.html
{{p.name}}
<img src="{{p.title_img.url}}">
the name is displayed while image is not displayed. In console, I get
"GET /media/p2.jpg HTTP/1.1" 404 2690
I changed some settings and was able to resolve it. Probably the mistake was that I was changing project_root/products/urls.py instead or project_root/urls.py. Overall changes I did:
In main urls.py, added these
urlpatterns = [
path('', ProductList.as_view() , name="product"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Changed settings.py
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
MEDIA_URL = '/media/'
As per the documentation: https://docs.djangoproject.com/en/3.1/howto/static-files/#serving-static-files-during-development and https://docs.djangoproject.com/en/3.1/howto/static-files/#serving-files-uploaded-by-a-user-during-development
it should be used only for development purposes,
settings.DEBUG must be set to True,
it works only if the url is local
in your case, 1 and 3 seams ok but is your DEBUG setting set to True?
As per the documentation: https://docs.djangoproject.com/fr/3.1/ref/contrib/staticfiles/#runserver, you can runserver with argument --insecure so you can still serve static files or media file with DEBUG set to False.
Related
I am using Django 3.1.
settings.py looks like
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = '/media/'
urls.py looks like
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my model image field looks like
dev_image_one = models.ImageField(upload_to='dev/', blank=True, null=True, verbose_name = 'image 1')
When files are uploaded, they end up in the /media/dev directory.
When they are to be displayed, the url looks like:
<img src="dev/1.png">
If I manually append /media/ on to the front of the url, the image displays. I never had this problem before so I'm at a loss as to what is going wrong. I never used 3.1 before, so I'm wondering if that doesn't have something to do with it. No problem with the static files. Thanks.
Make sure settings.py be like
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = '/media/'
load {% load static %} in any page that you want to access image
use {% static 'images/india.jpg' %} to acess image
In Django 3 I have this in my settings: MEDIA_URL = '/media/'
My MEDIA_ROOT echos out properly (/home/bradrice/source/repos/bb_backend/media
) and I have this in my urls:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I am using a VersatileImageField and the image uploads to the proper media folder. However, I can't see the image in Admin and if I click the link it prepends the Url with the full MEDIA_ROOT instead of just the /media/ onto the display url.
What am I doing wrong.
My error. In my models I had this code to make the upload path:
def artwork_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT / user_<id>/<filename>
return MEDIA_ROOT + '/artwork/{0}'.format(filename)
then in my image model I had upload_to=artwork_directory_path.
I just hardcoded it to upload_to="artwork" and everything started working.
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')
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)
I am trying to use django-photologue, I installed it and did mentioned settings.
I have a model Blog as follows:
class Blog(models.Model):
title = models.CharField(max_length=150)
thumbnail = models.ForeignKey(Photo, related_name='blogs')
I am able to add images to blog objects, however I do not see the thumbnail in my admin interface (clicking on it basically opens base.html stored in templates folder which I simply copied from example_project, this base.html is not important for me, however seeing this thumbnail could be interesting):
NOTE: I guess my MEDIA_ROOT and MDIA_URL properties are wrong, I am not sure what I should be writing there. I get
GET http://127.0.0.1:8000/photologue/photologue/photos/cache/dog_1_admin_thumbnail.jpg 404 (Not Found)
for
MEDIA_ROOT = os.path.join(BASE_DIR, 'photologue', )
MEDIA_URL = '/photologue/'
error, on my console.
My folder structure:
The thumbnails part of the question is fixed by doing following settings:
In my urls.py file I had to
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
#all urls
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And in my settings.py file I added:
STATIC_ROOT = os.path.join(BASE_DIR, 'public', 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'public', 'media')
MEDIA_URL = '/media/'