I am creating a website that should display several images, but none is displayed.
I have tried several ways to solve it but no changes,
image's URL is saved in sqlite db
views.py
def add_story(request):
if request.method == "POST":
form = StoryCreate(request.POST, request.FILES)
if form.is_valid():
storyitem = form.save(commit=False)
storyitem.save()
else:
form = StoryCreate()
return render(request, 'story_form.html', {'form':form})
urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root= settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)
settings.py
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = os.path.join(SITE_ROOT, 'statics')
MEDIA_URL = '/media/'
html
<img name="Story" style="height: 100px; width: 100px" src="{{item.LogoUrl.url}}"/>
Related
My django project is searching for images in
/profile/<pk>/myapp/profile_pics/images.jpg
instead of myapp/profile_pics/images.jpg
similar to this question image isn't uploaded to media root using Django
settings.py -
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py -
urlpatterns = [
path('profile/<int:pk>/', views.profile, name='profile'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
views.py-
def profile(request, pk):
user = get_object_or_404(User, pk=pk)
return render(request, 'myapp/profile.html',{'account': user})
Where's the problem?
I have looked this questions from the stack overflow.
question 1
question 2 about showing an image field in a template and follow accordingly. But the image is not showing in the template although the image is properly uploaded to the folder.
models.py
class Book(models.Model):
book_thumbnail = models.ImageField(upload_to='images/%Y/%m/%d', blank=True, null=True)
views.py
def home(request):
book = Book.objects.all()
return render(request, 'books/books.html', context={'books': book})
template.html
<img class="group list-group-image" src="{{ books.book_thumbnail.url }}" width='240' alt="alt" />
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py
urlpatterns = [
path('books/', views.home),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
printing media root works for me.
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
print(MEDIA_ROOT)
Here is what i just edited in settings.py
I have tried everything but somehow i cannot see images uploaded by user.I have deployed code on heroku and every static file is getting loaded properly. User is even going to correct path of image but somehow server is showing errors of not finding image.
Settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
#STATICFILES_DIRS = (
# os.path.join(PROJECT_ROOT, 'static'),
#)
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Models.py
#this is for photos upload by user
class You(models.Model):
user = models.ForeignKey(User,null=True)
name=models.CharField(max_length=30,blank=True,default='pictures/videos')
docfile = models.FileField(upload_to='documents/%Y/%m/%d', null=True,blank=True)
def __unicode__(self):
return self.user.username
#Views.py(To upload files)
#login_required
def upload(request):
if request.method == 'POST':
newdoc = You(docfile = request.FILES['file'],user=request.user)
newdoc.save()
msg='dee'
# Redirect to the document list after POST
return HttpResponse(json.dumps({'message': msg}))
else:
form = DocumentForm()
# Render list page with the documents and the form
return render(request,'mat_upload.html',{'form':form})
Template(gallery.html)
{% for document in documents %}
<img class="img-responsive" src="{{MEDIA_URL}}/media/{{ document.docfile }}" alt="hiiii" style="max-width: 100%;
height: 250px;" />
{% endfor %}
#login_required
def gallery(request):
documents = You.objects.filter(user=request.user)
return render(request,'gallery.html',{'documents': documents,})
Urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.views.generic import RedirectView
admin.autodiscover()
import harpoons.views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('harp.urls')),
url(r'^password_reset_recover/', include('password_reset.urls')),
url(r'^accounts/', include('allauth.urls')),
]
You cannot save user-uploaded files locally on Heroku. The filesystem is ephemeral, and does not persist across dyno restarts or between concurrent dynos. You need to upload them somewhere more permanent; a popular choice is Amazon S3, and there are plenty of libraries that will do that for you.
I'm following the django page which explains how to display images https://docs.djangoproject.com/en/1.6/howto/static-files/
And I think that I did all what's specified such as:
settings.py
STATIC_ROOT = BASE_DIR + '/static'
STATIC_URL = '/static/'
INSTALLED_APPS += ('django.contrib.staticfiles',)
models.py
image = models.ImageField(upload_to="static/my_app/")
def image_tag(self):
if self.image:
return u'<img src="{0}" />'.format("/" + self.image.url)
urls.py
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
admin.py
list_display = ('description', 'image_tag',)
But my image is not showing up.
What am I missing?
I have read Django {{ MEDIA_URL }} blank and How do I include image files in Django templates? questions but error still here: MEDIA_URL is empty in template.
My code:
settings.py:
MEDIA_ROOT = "D:/blizzard/Projects/Python/Web/moz455/app/media" # or os.path.join(SiteDir, "app/media")
MEDIA_URL = '/media/'
STATIC_ROOT = 'F:/Soft/Python26/Lib/site-packages/django/contrib/admin/static'
STATIC_URL = '/static/'
views.py:
def gallery(request):
ResponseDict = {}
return render_to_response('gallery.html', ResponseDict,
context_instance = RequestContext(request))
urls.py:
urlpatterns = patterns('',
(r'^gallery/', gallery),
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
) + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
Make sure you have included django.core.context_processors.media in your TEMPLATE_CONTEXT_PROCESSORS:
TEMPLATE_CONTEXT_PROCESSORS = (
...
"django.core.context_processors.media",
...
)