3
You here pass '{{photo.path}}'
as a string to {% static ... %}
, hence it will simply prepend the static URL root to this string.
If you want to use the content of photo.path
, you can use:
<img src="{% static photo.path %}"/>
So {% static ... %}
accepts variables as parameters, and will take the content of the path
attribute of the photo
variable. (of course given that variable is passed, or is a variable you generate with {% for ... %}
loops, etc.
2
Do it like this:
<img src="{%static 'images/'%}{{image_ex.png}}">
Use it after the static tag scope ends.
- [Django]-Delete field from standard Django model
- [Django]-Reverse for account_login not found in Django
- [Django]-Django mysql error
1
Uses a for tag
{% for p in photos %}
<img src="{% static '{{ p.path }}' %}"/>
{% endfor %}
Better uses Imagefield as field in your model
In your settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Create a folder named βmediaβ in your project (at the same level that your apps)
In your urls.py (main)
from . import views, settings
from django.contrib.staticfiles.urls import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In your models.py
Replace the CharField with Imagefield
image = models.ImageField(upload_to="my_folder_name")
Like this:
class Photo(models.Model):
title = models.CharField()
image = models.ImageField(upload_to="my_folder_name"))
In your views.py
def gallery(request):
photos = Photo.objects.all()
return render(request, 'gallery.html', {'photos': photos})
In your templates
{% for p in photos %}
<img src="{{ p.photo.url }}"/>
{% endfor %}
- [Django]-How to make django-crontab execute commands in Docker container?
- [Django]-Django save override ImageField handling
- [Django]-Django Rest Framework Send verification email
- [Django]-Unique email constraint for django.contrib.auth.user
- [Django]-Pillow Resizing images (thumbnail) on Amazon S3 β Django
0
For Template
{% for image in all_image %}
<img src="{{ image.image.url }}"/>
{% endfor %}
- [Django]-ModuleNotFoundError: No module named 'polls'
- [Django]-Implement get_serializer in generic list view (generics.ListCreateAPIView) in Django REST Framework
- [Django]-Django Writable Nested Serializers Update
- [Django]-Why are form field __init__ methods being called on Django startup?
0
Solved: load image dynamically in survey.html
"survey.toolsTechnology" is a dynamic variable, every time the
variable changes, the image also changes based on it.
<img src="{% static 'images/'%}{{survey.toolsTechnology}}.png"/>
- [Django]-Django-python how to convert text to pdf
- [Django]-How to add custom field in manytomany through table in Django
- [Django]-Django does not find Pillow though installed
- [Django]-UserWarning: Module _mysql was already imported from /usr/lib/pymodules/python2.6/_mysql.so
- [Django]-Writing multiple page pdf from html template using weasy print and django