I've deployed one project In web server. our project everything fine but i unable print image on profile page .in our local system working fine but in server error getting Not Found: /media/media/photos/e.jpg in django.
models.py
class Matr(models.Model):
photo = models.ImageField(upload_to='media/photos/', null=True,
blank=True)
settings.py
MIDIA_ROOT=os.path.join(BASE_DIR,'media')
MEDIA_URL='/media/'
urls.py
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
profile.html
{% if user.matr.photo %}
<img src="{{ user.matr.photo.url }}" width="330">
{% endif %}
Related
I'm hosting my site in railway. Everything is set up and works fine but the images uploaded by user don't load up.
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
MEDIA_URL ='/media/'
MEDIA_ROOT = os.path. join(BASE_DIR,'media')
models.py
class Post(models.Model):
img = models.ImageField(upload_to="pics")
blog.html
{% extends 'base.html' %}
{% load static %}
{% static "images/projects" as baseUrl %}
{% for post in post_list %}
<div class="image_wrapper"><a href="{% url 'post_detail' post.slug %}" target="_parent"><img
src="{{ post.img.url }}" alt="image 1"/></a></div>
{% endfor %}
urls.py
urlpatterns = urlpatterns + static(settings.MEDIA_URL,document_root= settings.MEDIA_ROOT)
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Error I'm getting:
Not Found: /media/pics/CC_Tech_Computers_1_6rgae2m.jpg
Not Found: /media/pics/CC_Tech_Computers_1_6rgae2m.jpg
Not Found: /media/pics/CC_Tech_Computers_1_6rgae2m.jpg
Not Found: /media/pics/CC_Tech_Computers_1_6rgae2m.jpg
instead of this:
src="{{ post.img.url }}"
Try this way:
src="/media/pics/post.img"
And in your models.py:
img = models.ImageField(upload_to="pics/") #Here added `forward slash (/)`
Try this way and see if it loads the image
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
I've deployed a project on a web server. In my project everything is working fine but I'm unable to print an image on the profile page. On my local environment everything is working fine but on the deployed version I'm getting the following error: Not Found: /media/media/photos/e.jpg in django.
models.py
class Matr(models.Model):
photo = models.ImageField(upload_to='media/photos/', null=True,
blank=True)
settings.py
MIDIA_ROOT=os.path.join(BASE_DIR,'media')
MEDIA_URL='/media/'
urls.py
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
profile.html
{% if user.matr.photo %}
<img src="{{ user.matr.photo.url }}" width="330">
{% endif %}
Try this
models.py
class Matr(models.Model):
photo = models.ImageField()
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media_root')
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)
profile.html
<img src="{{ user.matr.photo.url }}">
And make sure that after uploading photo, you have that photo in 'media_root' library.
I have two apps accounts and home in tutorial project.
A user can create a post in home app and I'm able to render post(i.e images and post data) in home app. In accounts app I want to create a search page where not logged in user can search and view the posts.
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'tutorial/media/')
search.html
{% load static %}
{% if re %}
{% for q in re %}
<a href="{% url 'home:Post_detail' pk=q.id %}">{{q.id}}.{{ q.post }}
<br>
<img src="{{ q.image.url}}" width="240" height="auto">
<p>Posted by {{ q.user.get_full_name }} on {{ q.created }}</p>
{%endfor%}
{% endif %}
I use the above code in both home app and accounts app. It works fine for home app but not for accounts app
when I see view source page I get perfect url in accounts app (search.html) but, the images are not loading properly screenshot
project urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('accounts/', include('accounts.urls' , namespace='accounts') ),
path('home/', include('home.urls' , namespace='home') ),
path('admin/', admin.site.urls),
path('',login_redirect , name = 'login_redirect'),
]
if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
tutorial project structure
tutorial
|_accounts
|_home
|_tutorial
|_media
|_profile_images
|_all image files
I have some photos uploaded but I was unable to see photos in temeplates
My template
{% extends 'base.html' %}
{% load staticfiles %}
{% block content %}
<div class="row ">
<div class="col-md-8">
<h1>{{ object.title }}</h1>
{% if object.productimage_set.count > 0 %}
<div>
{% for img in object.productimage_set.all %}
{{ img.image.file }}
{{ img.image.url }}
<img class='img-responsive' src='{{ img.image.url }}' />
</div>
{% endfor %}
</div>
{% endif %}
<div class="lead">{{ object.description }}</div>
</div>
</div>
My model
class Product(models.Model):
title = models.CharField(max_length=120)
description = models.TextField(blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=20)
active = models.BooleanField(default=True)
def __unicode__(self): # def __str__(self):
return self.title
class ProductImage(models.Model):
product = models.ForeignKey(Product)
image = models.ImageField(upload_to='product/')
Setting for media
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static", "my_static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "static", "static_root")
STATIC_URL = '/media/media_root/'
MEDIA_ROOT = os.path.join(BASE_DIR, "static", "media_root")
My urls
urlpatterns = [
url(r'^contact/', views.contact, name='contact'),
url(r'^$', views.ProductViewList.as_view(), name='ProductViewList'),
url(r'^(?P<pk>\d+)/$', views.ProductDetailView.as_view(), name='Product_Detail'),]
My folder structure
In the template {{ img.image.url }} gives -> product/mp3.jpg. But still I'm unable to see the image
The console shows that image is loaded from http://127.0.0.1:8080/product/1/product/mp3.jpg I guess there is some prob with the url to collect the image but not sure
Any help is much appreciated....Thanks in advance
You should add the MEDIA_URL to your settings.py, if you haven't.
In your case, the MEDIA_URL should be media_root. My Image links all have /media/....jpg.
In addition, you can specify the URLs:
from django.conf import settings
if settings.DEBUG:
# static files (images, css, javascript, etc.)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
Here's some documentation from Django on this: https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-files-uploaded-by-a-user-during-development
I prefer keeping my media files separate from my static files, so during development I actually store mine in the project root. The following snippets use the project root as the media location, but you can easily adjust this by changing it to static/media_root if you'd prefer to keep your current location.
First make sure you've defined your media root in your settings.py file:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Then try adding the following to your project's urls.py file:
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('', (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}))