django 1.10 media image not showing up - django

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')

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

Media Files not Loading on pythonanywhere.com (Django)

website: https://willpeoples1.pythonanywhere.com/
I deployed a personal portfolio project using pythonanywhere.com's server and my instructor sent me a video explaining why my media files weren't loading properly (https://d.pr/v/RARFcF). However, I couldn't understand why he was telling me to change the pathname of the image in my HTML b/c I am using a template in the img-tag. Any clue where I should go from here? Thanks
settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URLS = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
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
from portfolio import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('blog/', include('blog.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
home page HTML file:
<div class="conatainer text-center">
{% for proj in Projects %}
<hr>
<h2>{{ proj.title }}</h2>
<p>{{ proj.description }}</p>
<img src= "{{ proj.image.url }}" target="_blank" height="300" width="300">
<br>
<br>
{% endfor %}
</div>
This is all set. There was a spelling mistake in my settings.py file. I wrote "MEDIA_URLS" instead of "MEDIA_URL".

In Django project Media folder is not getting created

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..

how to show picture in the html by django

I can not show the picture in the html by django.
There is my project
This my project setting
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static/')
urls
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns=[
path('',views.main_view,name='main_view'),
]
urlpatterns += staticfiles_urlpatterns()
main.html
<img src="{% static '/my/1.jpg' %}" alt="My image">
There is my result.As you can see,the picture is desapper
setting.py
MEDIA_URL = '/pic/'
MEDIA_ROOT = os.path.join(BASE_DIR,'pic/')
urls.py
urlpatterns+=static('/pic/', document_root=settings.MEDIA_ROOT)
main.html
<img src="pic/1.jpg" alt="My image">
you just should to remove slash, because, in the settings STATIC_URL already has it.
<img src="{% static 'my/1.jpg' %}" alt="My image">
<!-- ^^^ -->
and by docs check DEBUG settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
This helper function will only work if DEBUG is True
and try to use standard static instead of staticfiles_urlpatterns
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

images from media file wont get displayed in django template

I'm new to django and I've been playing around with uploading pictures then displaying them. ... well trying to display them.
Whenever I try to display the image from a template, I just get the broken image link icon.
I'm using the sqlite3 server
settings.py
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
def location(f):
return os.path.join(ROOT_DIR, f)
MEDIA_URL = 'http://127.0.0.1:8000/media/'
MEDIA_ROOT = location('media/')
models.py
class Image(models.Model):
image = models.ImageField(upload_to = 'images/')
views.py
from imageupload.settings import MEDIA_ROOT, MEDIA_URL
def main(request):
imgs = Image.objects.all()
return render_to_response('index.html', {'images': imgs, 'media_root': MEDIA_ROOT, 'media_url': MEDIA_URL})
url.py
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Right now I've just used the admin to upload images. And that seems to work fine, they go to where I expect
But when I try to display them:
template
<!DOCTYPE html>
<html>
<img src="<correct path to project>/media/images/photo_1.JPG" />
{% for img in images %}
<img src="{{ media_root }}{{ img.image.name }}" />
<img src="{{ media_url }}{{ img.image.name }}" />
<img src="{{ img.image.url }}" />
{% endfor %}
</html>
I get the broken icon for each one.
The browser source code shows me:
<!DOCTYPE html>
<html>
<img src="<correct path to project>/media/images/photo_1.JPG" />
<img src="<correct path to project>/media/images/photo_1.JPG" />
<img src="http://127.0.0.1:8000/media/images/photo_1.JPG" />
<img src="http://127.0.0.1:8000/media/images/photo_1.JPG" />
</html>
that makes sense, I only have one photo uploaded.
and if I copy one of the hard links and put it into some other html file ...it works
Oh FFS....
aperently it's
MEDIA_URL = '/media/'
NOT
MEDIA_URL = 'http://127.0.0.1:8000/media/'
...despite #'http://127.0.0.1:8000/media/' being written right next to it
working img link looks like so:
<img src="/media/images/photo_1.JPG" />
you definitely need the:
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
in the url file also
For me, I did the following two things:
Define media url in the settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and then in the project's urls.py, defined serving url for development and production purpose:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('django_app.urls')),
]
# Serving the media files in development mode
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
else:
urlpatterns += staticfiles_urlpatterns()
then you can reference the image in the media folder in the template like this:
<img src="media/path_to_image" />
Note: Don't forget to set the DEBUG flag to False in the settings.py for production.
if you are looking for display images in development environment try this:
settings.py
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(SITE_ROOT, 'static')
MEDIA_URL = '/static/'
STATIC_ROOT = os.path.join(SITE_ROOT, 'statics')
STATIC_URL = '/statics/'
urls.py
# somebody import this from settings
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$','django.views.static.serve',
{'document_root': os.path.join(SITE_ROOT, 'static')})
)
html view file *.html:
<img src="{{ MEDIA_URL }}img/content_top_edit_form.png">
this means we have a img folder in static folder. in your case you can change this by
if you see your browser html must be seen like this:
<img src="/static/img/content_top_edit_form.png">
in the template use {{img.image.name.url}}