Django | Error when insert imageName with for into static url - django

Error when we want to insert image file name into static url. No all has imageName.
Error:
Invalid block tag
Code:
<img width="40%" src="{% static 'files/img/
{% for i in questions %}
{% i.imageName %}
{% endfor %}
' %}">

This isn't built into Django but you can try something along these lines.
files = os.listdir(os.path.join(settings.STATIC_ROOT, "files/img"))
{% for file in files %}
<img width="40%" src="{{file}}">
{% endfor %}

try:
{% for i in questions %}
<img width="40%" src="{% static 'files/img/{{i.imageName}}' %}">
{% endfor %}

try
{% for i in questions %}
<img width="40%" src="{% static "files/img/{{i.imageName.url }}" %}">
{% endfor %}

Related

I want to separate products based on id but tempatesyntaxterror is shown

I tried this:
{% for i in object %}
{% if i.get(id=1) %}
<img src="{{object.img1.url}}" />
<p><b>Rs {{i.price}}</b></p>
{% endif %}
{% endfor %}
But, this error is shown:
TemplateSyntaxError
Could not parse the remainder: '(id=1)' from 'i.get(id=1)'
I believe you are trying to get the id of i and trying it to check with if statement. So, try
{% for i in object %}
{% if i.id == 1 %} <--- Here
<img src="{{ i.img1.url }}" /> <--- Here also i.img1.url not object.img1.url
<p><b>Rs {{i.price}}</b></p>
{% endif %}
{% endfor %}

How break cycle for with if else statement in django template

I read if else statement in Django docs
but i don't understand my case.
I have photos list, i want render image if is COVER else i want render static image.
This my code
{% for x in listing.photos.all %}
{% if x.photo_tipo == 'COVER' %}
<img src="{{ x.get_thumb }}" alt="">
{% else %}
<img src="{% static 'images/about/1.jpg' %}" alt="">
{% endif %}
{% endfor %}
Result is: an image if x.photo == 'COVER' and a static image for every other photo in the list.
I would like to get only one result if the declaration is true or only one static image if it is false
Don't do this in the template. Add some logic somewhere that gives you the photo with that type directly if it exists. A good way would be with a method on the Listing model:
class Listing(models.Model):
...
def cover_photo(self):
return self.photos.filter(photo_tipo='COVER').first()
Now your template could be:
{% with photo as listing.cover_photo %}
{% if photo %}
<img src="{{ photo.get_thumb }}" alt="">
{% else %}
<img src="{% static 'images/about/1.jpg' %}" alt="">
{% endif %}
{% endwith %}

Django Block Heritage

I am trying to modify the part {% block contentLeft%}.
However, I can not change this part only. When I use {{super}} I get the full block but I can not change it.
I can not edit a block in a block.
I used includes, I tried to remove the block tags for the body however I can not modify the tag contentLeft, contentCenter, contentRight
I am also looking to change the name of modules in the administration page, do you have an idea? I changed the name of the models but I can not change the name of the "application"
Default value it's not a good solution (i post a sample exemple)
{% extends "base.html" %}
{% block title %}Ma page Date{% endblock %}
{% block body %}
{{ block.super }}
{% block contentLeft %}
essai
{% endblock %}
{% block contentCenter %}
centre
{% endblock %}
{% block contentRight %}
droite
{% endblock %}
{% endblock %}
{% load static %}
{% block body %}
<body>
{% block header %}
{% include 'header.html' %}
{% endblock %}
<div class="container-fluid">
<div class="row">
<div class="col-md-2">
{% block contentLeft %} Toto1 {% endblock %}
</div>
<div class="col-md-8">
{% block contentCenter %} Toto2 {% endblock %}
</div>
<div class="col-md-2">
{% block contentRight %} Toto3 {% endblock %}
</div>
</div>
</div>
{% block footer %}
{% include 'footer.html' %}
{% endblock %}
<script src="{% static 'js/jquery-3.2.1.min.js' %}"></script>
<script src="{% static 'js/bootstrap.js' %}"></script>
<script src="{% static 'js/popper.min.js' %}"></script>
<script src="{% static 'js/mdb.js' %}"></script>
</body>
{% endblock %}
<!DOCTYPE html>
<html lang="fr">
{% block head %}
{% include 'head.html' %}
{% endblock %}
{% block body %}
{% include 'body.html' %}
{% endblock %}
</html>

Display static images with url from dict in template

I'm having some trouble displaying my images if the url value comes from a dict.
if I do below it works.
{% for key, values in images.images.items %}
<div class="imageBox">
<img src="{% static 'images/ka/1.jpg' %}" alt="My image" width=100% height=100%/>
</div>
{% endfor %}
But I'd like to use the url values from the dict iteration instead. Like below
{% for key, values in images.images.items %}
<div class="imageBox">
<img src="{% static '{{values}}' %}" alt="My image" width=100% height=100%/>
</div>
{% endfor %}
value
{{values}} = 'images/ka/1.jpg'
But this dosen't work even tho {{values}} is 'images/ka/1.jpg' like the previous example. If I inspect the html in my browser it shows the src is /static/%7B%7B%20values%20%7D%7D. Why is it different?
You don't need {{ }} inside {% static %} tag.
Try this:
{% for key, values in images.images.items %}
<img src="{% static values %}">
{% endfor %}
This should work.
You can use the get_static_prefix template tag like so:
<img src="{% get_static_prefix %}{{ values }}" alt="My image" width=100% height=100%/>
get_static_prefix populates a template variable with the path specified in your STATIC_URL.
Reference get_static_prefix

Starting with Twig templates in Symfony2

I have this twig template:
{% block javascripts %}
{% javascripts '#AibFrontendBundle/Resources/public/js/update.js' %}
<script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.js'></script>
<script src="{{ asset_url }}" type="text/javascript"></script>
{% endjavascripts %}
{% endblock %}
{% block content %}
<div id="contents">
{{ contents|raw }}
</div>
<form action="{{ path('homepage') }}" method="post">
{{ form_widget(form) }}
<input type="submit" />
</form>
{% endblock %}
It shows correctly the content.
But if I add this line below at the begining
{% extends 'AibFrontendBundle::layout.html.twig' %}
It shows layout.html.twig's content but the content I mentioned above is not showed any more..
How to show that content again?
Your layout.html.twig template should include something similar to the following:
{% block content %}some optional default content here{% endblock %}
which will be replaced by whatever you supply in your page template:
mypage.html.twig:
{% block content %}
This will appear in layout.html.twig where I specified the above block
{% endblock %}
See the Twig documentation for further details