Reputation: 115
I am trying store a set of files manually in media folder and access them through path in a template. Here goes my settings for media
MEDIA_URL="/media/"
MEDIA_ROOT=os.path.join(BASE_DIR,"media")
Here is my project urls
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('books.urls')),
]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
Here goes my template code
<div class="category-img">
<img src="/media/logo.png" alt="">
</div>
And there is no image displayed .Media folder has logo.png file in it.I got following error.
Internal Server Error: /media/bordered.jpg/
Traceback (most recent call last):
File "C:\Users\Sriram\anaconda3\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Sriram\anaconda3\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Sriram\Desktop\books_site\books\views.py", line 42, in bookinfo
book["description"]=descriptions[zonar][books[zonar].index(book_name)]
KeyError: 'media'
Upvotes: 1
Views: 9020
Reputation: 151
In your template, where you want to set the image appearance use the code
<img src="{% get_media_prefix %}{{uploaded_file}}" alt="File 2">
Where uploaded_file
is the name of your image file. This is assuming your files are uploaded in the root media folder.
Upvotes: 2
Reputation: 535
You need to use {% get_media_prefix %}
tag.
you have to set the MEDIA_ROOT
and the MEDIA_URL
in your settings and add the MEDIA_URL
to your urls.py like in this document. and change your template code like this:
{% load static %}
....
<div class="category-img"><img src="{% get_media_prefix %}logo.png" alt=""></div>
this tag load correctly URL to your media file path setup in Django setting. if you want to deploy your project in server or use mount external server to load your media, need to change only media settings in your django project.
best practice to save image you want to use as static in your web site, save this type of file in your static folder and use media folder in django project to save all uploaded file in your web application.
and load them with pass your Django model object to the template and load the URL with {{object.field.url}}
tag in Django template.
Upvotes: 5
Reputation: 1707
Media files is for files which are added through the database. Use static for files which don't change. I also don't understand what's the problem if there is PDF files. Why can't you put that in static?
But anyway, there is no problem with your setup. The problem is in a function bookinfo
in views.py
. It is saying that there is a KeyError
, which means that you are accessing something in a dictionary which doens't exist. My guess is that zonar
is set to "media"
or books[zonar].index(book_name)
results to "media"
. Correct that error, and the media files should be served properly.
Upvotes: 2
Reputation: 84
Try using static:
STATIC_URL="/static/"
STATIC_ROOT=os.path.join(BASE_DIR,"static/")
And store the image files in the static folder and it should be inside the django application folder.
--static/
--img/
--logo.png
{% load static %}
<div class="category-img">
<img src="{% static 'img/logo.png' %}" alt="">
</div>
Upvotes: 1