I have a very simple question, on which i spent hours.
Heres the deal: i insert a path to an image in mongodb, save said image in my static folder (under objectId.png) and pass the objectId to my template in order to display it.
Heres some code, in views:
return render(request,'resultat_simulation.html',{'truc':truc2})
truc2=str(truc.id), truc being the object inserted in the DB.
It is passed to the template without issue.
Now, the template:
{% extends 'base.html' %}
{% load static %}
{% block body %}
{{truc}}
<img src="{% static 'C:\Users\smeca\Documents\projetfinal\stage\app\static\images\{{truc}}.png' %}">
{% endblock %}
If instead of {{truc}}, i put the ObjectId of any image, it is rendered.
I just want to append the string form of objectid to '.png' so i can loop through the images and display them all.
I tried everything i found on the net to no avail.
Thanks in advance.
{% with 'stage/app/static/images/'|add:truc|add:".png" as image_static %}
{{image_static}}
<img src="{% static image_static %}">
{% endwith %}
{{image_static}} returns .png
Solution :
Finally i straight up passed the path to the template
return render(request,'resultat_simulation.html',{'image_path':'/images/' + str(truc2) + '.png'})
and loaded it with {% static image_path %}
You can not use a variable with double brackets {{ }} inside template logic {% %}.
The workaround in order to use a variable along a static tag is to use the add tag:
{% with "first/part/of/your/path"|add:truc|add:".png" as image_static %}
<img src="{% static image_static %}">
{% endwith %}
Related
I am doing a django book store project which has a successful sign up function. I followed WS Vincent's tutorial to connect Github signup (https://learndjango.com/tutorials/django-allauth-tutorial) and I have come across an error when integrating it.
TemplateSyntaxError at /
Invalid block tag on line 16: 'provider_login_url', expected 'endif'. Did you forget to register or load this tag?
Below is the code for the major areas of my project.
home.html
{% extends '_base.html' %}
{% load static %}
{% block title %}Home{% endblock title %}
{% block content %}
<h1>Homepage</h1>
<img class="bookcover" src="{% static 'images/djangoforprofessionals.jpg' %}">
{% if user.is_authenticated %}
<p>Hi {{ user.email }}!</p>
<p>Log Out</p>
{% else %}
<p>You are not logged in</p>
<p>Log In |
Sign Up
Sign Up</p>
{% endif %}
{% endblock content %}
I am not familiar with HTML but I take the two sign up lines are not meant to be put one after the other.
According to the documentation, it seems like you forget to load {% load socialaccount %}.
Reference
Please Consider the following pieces of code.
<!--templates/home.html-->
{% extends 'base.html' %}
{% load static %}
{% block content %}
{% for post in object_list %}
<div class = 'post-entry'>
<h2><a href="{% url 'post_detail' post.pk %}">{{ post.title }}</h2>
<p>{{ post.body }}</p>
</div>
{% endfor %}
<script type = "text/javascript" src = "{% static 'js/test.js' %}"></script>
{% endblock content %}
and
<!--templates/home.html-->
{% extends 'base.html' %}
{% load static %}
{% block content %}
{% for post in object_list %}
<div class = 'post-entry'>
<h2><a href="{% url 'post_detail' post.pk %}">{{ post.title }}</h2>
<p>{{ post.body }}</p>
</div>
{% endfor %}
{% endblock content %}
<script type = "text/javascript" src = "{% static 'js/test.js' %}"></script>
The first one executes successfully but the second one does not. Is it necessary to load an external static file from inside a django template block and if not then why does the second code not execute?
PS: I am new to django.
For purpose of clarity i am also providing the code for the base template here.
<!--templates/base.html-->
{% load static %}
<html>
<head><title>Django Blog</title>
<link href = "{% static 'css/base.css' %}" rel = "stylesheet">
</head>
<body>
<header><h1>Django Blog</h1></header>
<div>
{% block content %}
{% endblock content %}
</div>
</body>
</html>
The first one executes successfully but the second one does not. Is it necessary to load an external static file from inside a django template block and if not then why does the second code not execute?
If you override a basic template, you can only "fill the blocks" so to speak. Where is Django supposed to write the things you write outside the blocks? At the beginning of the file? At the end of the file? Somewhere in between?
As specified in the documentation on template inheritance [Django-doc]:
The most powerful – and thus the most complex – part of Django’s template engine is template inheritance. Template inheritance allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override.
You can however define multiple blocks. For example it is common to add a block at the end where you can optionally add some extra JavaScript in, like:
<!--templates/base.html-->
{% load static %}
<html>
<head><title>Django Blog</title>
<link href="{% static 'css/base.css' %}" rel="stylesheet">
</head>
<body>
<header><h1>Django Blog</h1></header>
<div>
{% block content %}
{% endblock content %}
</div>
{% block js %}
{% endblock %}
</body>
</html>
So then you can write the <script ...> part at the bottom of the page, like:
{% extends 'base.html' %}
{% load static %}
{% block content %}
{% for post in object_list %}
<div class='post-entry'>
<h2><a href="{% url 'post_detail' post.pk %}">{{ post.title }}</h2>
<p>{{ post.body }}</p>
</div>
{% endfor %}
{% endblock %}
{% block js %}
<script type="text/javascript" src="{% static 'js/test.js' %}"></script>
{% endblock %}
You can of course define variables, etc. outside the {% block ...%} ... %{ endblock %} parts. But everthing that is rendered outside is ignored if you inherit from a base template.
for the second script, you are specifying the src attribute of the script element using a django command {% static 'js/test.js' %}, for that to work it needs to be inside a django block,
if you want to do this without using the django block, you need to specify the value of the src attribute without using a django commande, you should do it just as if you are working only with html,
<script type = "text/javascript" src = "path_to test.js"></script>
I have two templates that go together, one inside the other. The inner one has an icon that needs to change according to the content of the parent template.
I've tried to pass the icon path using a variable:
src="{% url 'main_bar_icon' %}">
and I added this line of code in the parent template:
{% with main_bar_icon='../static/dist/img/logout-icon.svg' %}
{% include 'main_bar.html' %}
{% endwith %}
So, this is my inner template:
{% block main_bar %}
<a href="">
<img class="app-main-bar-icon"
src="{% url 'main_bar_icon' %}">
</a>
{% endblock main_bar %}
And this is my parent template:
{% block content %}
{% with main_bar_icon='/dist/img/logout-icon.svg' %}
{% include 'main_bar.html' %}
{% endwith %}
{% endblock content%}
In the browser I get this:
<img class="app-main-bar-icon" src(unknown) alt="icon">
Unfortunately, the {% url ... %} templatetag, can be only used to retrieve urls for the views defined in the urlpatterns.
For your needs, you will need either:
use plain variable, since you already assign path to the variable, simply: <img class="app-main-bar-icon" src="{{ main_bar_icon }}">
or, for more future proof solution, you can configure django static files and use {% with main_bar_icon='dist/img/logout-icon.svg' %} and then <img class="app-main-bar-icon" src="{% static main_bar_icon %}">
If I am passing in a variable to a template via an include...
{% include "module.html" with id=someVar %}
on the other side, how can I place it inside a dot noted path in order to call an object from a source?
<img class="brand-logo" src="{% static data.someVar.logo %}">
I was just over thinking it - all I had to do was pass in the following:
{% for module in modules %}
{% include "module.html" with data=module %}
{% endfor %}
Then I can use:
<img class="brand-logo" src="{% static data.content.logo %}">
It's my first approach to matplotlib.
I have been trying for a while to print my plots in a template. I know how if I do this for just one image, but now I'm trying to modify it and call different images.
I got this and it works perfectly:
<img src="{% static "/consulta/imagenes/rosavientos.png" %}">
But I'm trying to use this path: "/consulta/imagenes/rosavientos.png" like this:
<img src="{% static {{ my_path }} %}">
But ID or literal expected after static.
Is it possible to do this in any way?
PS: I also tried this:
In my view:
ruta_estatica = "<img src = \"{% static '/consulta/imagenes/" + nombre_png + ".png' %}\">"
In the template:
{% autoescape off %}{{ respuesta3 }}{% endautoescape %}
You can use the with tag to create a variable and use that in the static tag
{% with '/consulta/imagenes/'|add:nombre_png|add:'.png' as image %}
<img src="{% static image %}"/>
{% endwith %}
As I had the same problem and the provided answer by #Brobin was a little bit confusing to me I'll try to clarify it a little bit more.
Considering you have the variable:
{{ my_path }}
and you want to use it inside the function:
<img src="{% static 'some path here...' %}">
you can directly input the variable inside the static function without the {{ }} as follows:
<img src="{% static my_path %}">
Now lets assume you want more complex processing like having an 'item' from the 'my_path' as if it was a list of paths and also include some other text to that as a path or file type, you'll have to use the with function:
{% with final_path='MainSite/videos/My_Sample_Video_'|add:my_path.0|add:'.mp4' %}
<source src="{% static final_path %}" type="video/mp4">
{% endwith %}
Hope that helps.