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="" />
Related
I'm looping through each message which has a particular user which in intern as an image associated with it.
<img src="{{message.user.avatar.url}}" />
I want to convert it something like this (which I know is obviously very wrong)
<img src="{% static {{message.user.avatar.url}} %}" />
Is there a way to do so where I can find the equivalent working command of the above code?
Try using with
{% with avatar_url=message.user.avatar.url %}
<img src="{% static avatar_url %}" />
{% endwith %}
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!
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 want to show the user's image
I try with:
{% load static from staticfiles %}
<img src="{% static Logos/{{user.photo}}.png %}" />
and
{% load static from staticfiles %}
<img src="{% static Logos/{% user.photo %}.png %}" />
but I get this error:
TemplateSyntaxError at Could not parse the remainder
You can not have {{.. or {%.. within a {%..%}. Hence the error.
You can use get_static_prefix instead
{% load static %}
<img src="{% get_static_prefix %}{{user.photo}}.jpg" />
OR
STATIC_URL by including the appropriate context processor, which is django.core.context_processors.static
So the usage would be :
{{STATIC_URL}}{{user.photo}}.jpg
I have created a custom inclusion template tag that accepts a single Update model object.
Template tag:
#register.inclusion_tag('update_line.html')
def update_line(update):
return {'update': update}
update_line.html:
<tr><td class="update">{{ update }}</td><td class="ack">
<img id="update-{{ update.pk }}" class="ack-img" src="{{ STATIC_URL }}img/acknowledge.png" alt="Acknowledge" /></td></tr>
The problem is that {{ STATIC_URL }} is not available in my inclusion template tag template, even though I am using the django.core.context_processors.static context processor so {{ STATIC_URL }} is available to all of my 'normal' templates that aren't being processed through an inclusion template tag.
Is there a way I can get the STATIC_URL from within my inclusion template tag template without doing something nasty like manually getting it from settings and explicitly passing it as a context variable?
Okay. Just figured this out after posting the question:
Instead of using {{ STATIC_URL }} in my inclusion template, I use the get_static_prefix tag from the static template tags:
update_line.html:
{% load static %}
<tr><td class="update">{{ update }}</td><td class="ack">
<img id="update-{{ update.pk }}" class="ack-img" src="{% get_static_prefix %}img/acknowledge.png" alt="Acknowledge" /></td></tr>
Update
I believe the correct way to do this now (django 1.5+) is:
update_line.html:
{% load staticfiles %}
<tr><td class="update">{{ update }}</td><td class="ack">
<img id="update-{{ update.pk }}" class="ack-img" src="{% static 'my_app/img/acknowledge.png' %}" alt="Acknowledge" /></td></tr>
Inside your template tag code, you can do what you like: so you can easily import STATIC_URL from settings yourself.