In Django project Media folder is not getting created - django

There is no media folder in the project, and i can see the media in db, but it won't load on template.
With this error:
Not Found: /DSC_0008.JPG
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('UserProfile.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
index.html looks something like this:
<div class="profile-userpic">
<img src="{{ my_details.associate_image.url }}" width="150" height="150" class="img-responsive" alt="my pic">
</div>

For development server,
urlpatterns = [
# ...
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This will create your media folder when DEBUG = True in your settings.
Make sure your image path {{ my_details.associate_image.url }} is correct. The media url must be like 127.0.0.1:8000/media/..filename.ext..

Related

how to show file in a template - django

I want to show an uploaded file :
{% for post in posts %}
<img src="{{post.image.url}}">
<h3>{{post.title}}</h3>
<button type="button" class="btn view-project-btn">view more</button>
<div class="item-details">
<div class="description">
<p>{{post.description}}</p>
</div>
<div class="info">
<ul>
<li>created - <span>{{post.date_creation}}</span></li>
<li>le theme de recherche -<span>{{post.sous_titre}}</span></li>
{{post.body|safe}}
<iframe src="{{post.file.url}}" width="100%" height="500px"></iframe>
</ul>
....
{% endfor %}
this is my views
def home(request):
posts = Post.objects.filter(active=True)
context = {'posts':posts}
return render(request,'base/index.html',context)
but it doesn't work it works for imagefield {{example.image.url}}, this is the attribute in my model file = models.FileField(null=True, blank=True,upload_to="sources")
thanks for your help and suggestions
the error :
Not Found: /sources/file1.pdf
urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('post.urls')),
path('ckeditor/', include('ckeditor_uploader.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root= settings.STATIC_ROOT)
in my settings
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
MEDIA_ROOT = os.path.join(BASE_DIR,'static/media')
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
here is the issue in your views your are posts in for calling the file but u are using post instead of posts
def home(request):
post = Post.objects.filter(active=True)
context = {'posts':post}
return render(request,'base/index.html',context)
change your view and you are good to go
or change your html code it's totaly upto you
add this in your mainprojects urls.py
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATICFILES_DIRS)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
try this
<embed
src="{{post.file.url}}"
type="application/pdf"
frameBorder="0"
scrolling="auto"
height="500px"
width="100%"
></embed>
edit media root
MEDIA_ROOT = os.path.join(BASE_DIR,'static/images')
this is wrong, you can't have media root in static root
remove static/ and try it again

Django is loading static files but not image that are stored from backend in production

As the title says, after I changed DEBUG to False, the image that is uploaded from the admin panel are not loading. However, other static files like css files and images that are loaded only from HTML side are rendered properly.
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('marathon.urls', namespace='homepage')),
path('accounts/', include('allauth.urls')),
path('logout', LogoutView.as_view(), name='logout'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
html file that renders image
<div class="js-billboard">
{% for slide in slider %}
<div class="billboard__item">
<figure><img src="{{ slide.image.url }}" alt="Mountain Marathon"></figure>
<div class="billboard__detail">
<div class="grid-container">
<div class="grid-x grid-padding-x">
<div class="col-12 cell">
<h2>{{ slide.title }}</h2>
<p><span>{{ slide.description }}</span></p>
{{ slide.link_title }}
</div>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
I assume you have the following line of code in your projects urls.py
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Please note that static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) for your media urls will only be added to your urlpatterns list if DEBUG=True in this case. Django doesn't serve media files in Production or when the DEBUG=False
I would suggest using a online storage API like AWS3, cloudinary or even firebase. These do offer the capability of serving media files whether DEBUG=True or DEBUG=False.
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
add this in your main your urls.py folder and create a forlder name staic cdn in you main project and in your settings.py
STATIC_CDN = BASE_DIR / 'static_cdn'
and in bottom
STATIC_ROOT = STATIC_CDN
# MEDIA
MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/'
and then run
python manage.py collectstatic
in your terminal what it will do it will create copy of you static file in your static cdn folder because cloud base storage look your static or media file in you cdn folder
and make sure you install pillow
and tell me if you still getting error

Django can not show image in folder

I create folder images path static/media/images/ and upload image 02.jpg to folder images by FTP. I want to show image 02.jpg to index.html but it's not show image. This is my code.
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
]
if settings.DEBUG:
urlpatterns+=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
urlpatterns+=static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
settings.py
#BASE_DIR is project location
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR, 'static', 'media')
index.html
{% load static %}
<img src="{% static '/images/02.jpg' %}" class="d-block w-100" alt="">
How to fix it ?
Now I can fix it by using.
<img src="/media/images/02.jpg" class="d-block w-100" alt="">

DJango read static file Json

I am learning django, I would like to read a json file but I do not kown where to put it cause I cannot display.
I have place the file in the folder "static_project" but it is not working
Here is the structure of my project:
src=>templates=> Main=> home.html
src=>Myproject=> settings.py
src=>Myproject=>urls.py
here the html:
{% load static %}
<lottie-player
src= src="{% static "./myfile.json" %}" background="transparent" speed="1" style="width: 700px; height: 700px;" loop autoplay >
</lottie-player>
</div>
Hereis the main urls.py
if settings.DEBUG:
import debug_toolbar
urlpatterns += [
path('__debug__/', include(debug_toolbar.urls)),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Here is my setting :
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static_project")
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "static_root")
#every time a user upload folder, it will be inside the static_cdn folder and the root "media root"
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "media_root")
Thanks for your help

django 1.10 media image not showing up

I am unable to display images uploaded by the admin user through admin on a template.
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
url.py
from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
import web.views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', web.views.home, name='home')
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
page.html
<img src="{{ event.image.url }}" alt="{{ event.title }}" />
also tried
<img src="{% get_media_prefix %}{{ event.image.url }}" alt="{{ event.title }}" />
I am not sure what I am missing. Thanks for your help.
--------------UPDATE--------------
I had change the home path to remove the regex which was causing the routing to match it before the media url.
url(r'', web.views.home, name='home')