Dynamic url for images on s3 in Django - django

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

Related

Is there a way to load avatar of each user using static method in Django?

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 %}

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 }}">

Django Template short hand for loading and using static files

For example to load a static image, I need to do that in template in two lines, like this:
{% load staticfiles %}
<img src="{% static "img/pro_pic.png" %}">
Is there any way to do that in one line?
<img src="{% staticLoad( "img/pro_pic.png" ) %}">
and a function somewhere that checks {% load staticfiles %} is there or not, if not it loads and all that? I also encourage time complexity discussion if it can be done!
By default the django.core.context_processors.static context process is included in the TEMPLATE_CONTEXT_PROCESSORS setting so you can just write in your template:
<img src="{{ STATIC_URL }}img/pro_pic.png">

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="" />