Jinja2 - Expressions concatenating issue - flask

I've got this variable
{% set img = Bicycles[Keys[i]][2] %} which is a string
I'm trying to add it to an img html element like this
<img src="{{ url_for('static', filename=' {{ img }} ') }}" class="card-img-top" alt="...">
My expected output when the page loads is
<img src="{{ url_for('static', filename='theurlhere') }}" class="card-img-top" alt="...">
But all i get is this "/static/%20%7B%7B%20img%20%7D%7D%20"
I'm so lost e_e

you need to concatenate the variable img. instead of using {{ img }} use '~img~'
This is a link to a similar question asked here
String concatenation in Jinja

Related

Django concat variable in template

i have this template on my Django
{% for car in cars%}
<div class="form-row">
<label for="detail-{{ detail.id }}"> <b>{{ detail.rejected_field.title }}</b> :
<img id="car-image" src="{{ car_manager_image_ + car.id }}">
</label>
</div>
{%endfor%}
car_manager_image is another image url sent via extra_context['car_manager_image_'+car.id] on the backend
my question is, how do i concate the data on Django Template?
im expecting result like 'car_manager_image_1'
You can use django build in add filter to concat both strings. Your code should be changed to
<img id="car-image" src="{{ car_manager_image_|add:car.id }}">

Django tag within URL tags

I would like to use dynamic images based on a a pk of the page.
To be clearer I have a survey app, using one question per page .. which mean that the user is presented a question he answers and is redirected to the next question. Each question has a different id and I would like to attach to each question an image.
Right now I have a static image lik that :
<div class="col-5">
<img src="{% static 'images/RightSpeech.jpg' %}" class="img-fluid" alt="Responsive image">
</div>
but I would like to make it dynamic.. I tried something like that with no success:
<div class="col-5">
<img src="{% static 'images/image{{question.pk}}.jpg' %}" class="img-fluid" alt="Responsive image">
</div>
any idea ?
The static tag doesn't really do anything more than adding the value of STATIC_URL to whatever you pass it. Instead of messing about with all these tags, you could just do this manually:
<img src="{{ STATIC_URL }}/images/image{{question.pk}}.jpg"
If for some reason the static context processor isn't activated, you can use the {% get_static_prefix %} tag in exactly the same way:
<img src="{% get_static_prefix %}/images/image{{question.pk}}.jpg"
You need to use
{% static 'images/image'|add:question.id|add:'.jpg' %}
in order to get concatenate the question PK to the image name. This just makes use of the concatenation filter |add:.
EDIT 1
If the above does not work, you can try to use the {% with %} tag, so in your case:
{% with "images/image"|add:question.id|add:".jpg" as customUrl %}
{% static customUrl %}
{% endwith %}
Hope this helps!

I can't add a picture from Django database through a variable

if i try to add a simple static address with this code:
<img alt="" class="img-responsive" src="{% static 'img/"Diamond Arshad.PNG"'%}">
it works,
but when i try this code:
{% for B in productss%}
<img alt="" class="img-responsive" src="{% static 'img/"{{B.image}}"'%}">
{% endfor %}
to get data from my database with the help of loop
it doesn't give me exact address:
it gives strange address while in my database there is the value is Diamond Arshad.PNG
Use the url property of your ImageField directly (i.e. without {% static %}:
<img src="{{ B.image.url }}">

I'm trying to create an url_for in an html <img src=...> tag where the filename for the image is stored in a variable. How would I do this? [duplicate]

This question already has an answer here:
Reference template variable within Jinja expression
(1 answer)
Closed 6 years ago.
I am trying to create a page for users that will display their profile pic and I've retrieved the picture filename from a database and stored that filename as a variable called "profilePic".
I've passed profilePic to the html page as a variable, but how would I create the <img> tag using Jinja2's {{ url_for }}. I know how to do it if I just type in the picname, but in this case the pic's name changes from user to user.
I tried...
<img src="{{ url_for('static', filename='img/user/{{ profilePic }}' }}>
which does not work because it appears you cannot nest {{ }} items in Jinja2.
How would I go about doing this?
You can just perform normal string concatenation in your template using +
<img src="{{ url_for('static', filename='/img/user/' + profilePic) }}" />
or .join
<img src="{{ url_for('static', filename='/'.join(['img', 'user', profilePic])) }}" />

Django - possible to display ImageField objects in static folder?

This is my settings.py:
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
'/home/userName/project/app/static',
)
Suppose my view is passing an ImageField object to the template. So as described here in the docs:
https://docs.djangoproject.com/en/1.5/ref/models/fields/#django.db.models.fields.files.FieldFile.url
I can access the URL of the image by doing
{{ FieldFile.url }}
in my template. It works if I try to access the URL of the image, however, I need to be able to load static as well. I want to do something like this:
{% load staticfiles %}
<img src="{% static "{{ FieldFile.url }}" %}" alt="" />
however, that does not work. Suppose the FieldFile.url is
images/imageName.jpg
and when I do
<img src="{% static "images/imageName.jpg" %}" alt="" />
it works, but it doesn't work when I use
{{ FieldFile.url }}
for some reason, how come?
Note: when I try to do
{% load staticfiles %}
<img src="{% static "{{ FieldFile.url }}" %}" alt="" />
and 'inspect element' using google chrome, it shows this
<img src="/static/%7B%7B%20FieldFile.url%20%7D%7D" alt="" />
The {{ }} are not necessary since you are already inside a template tag:
<img src="{% static FieldFile.url %}" alt="" />