You do not show me the image on my Website

0

Good, I've been learning django for a short time, my problem arises when I try to load the image associated with an object in the database.

{% extends 'index.html' %}
{% block titulo %}
Jugadores
{% endblock %}
{% block menu%}
{% if jugadores %}

<div class='col-sm-6'>
<hr>
{% load static %}
{% for jugador in jugadores %}   
<img src="{% static "jugador.imagen" %}">       
<p><h2>Nombre:{{ jugador.nombre }}</h2></p>
<p><h3>Edad:{{ jugador.edad }}</h3></p>
<p><h3>Posicion:{{ jugador.posicion }}</h3></p>

{% endfor %}
{% else %}
<p><h2>No hay jugadores</h2></p>
{% endif %}
</div>

<hr>
{% endblock %}
{% block header %}
{% endblock %}

My website looks like this:

My problem is that, when entering my image in the database, it generates a parallel static directory where I have my project and when calling that image in my templates, it does not find that image

    
asked by Rafael Romero Medina 25.01.2017 в 10:17
source

2 answers

1

You have verified the path of the image in the source code resulting from the web (in the browser's visualization). If you have the whole image in the database, you can not show it that way, unless you convert it to base-64 (see this ). If in the database you only have the name of the image, verify that the path is complete in the src="", otherwise it will search the image in the current directory.

Another annotation: you open the FOR inside the DIV and you close it out, which breaks the code of the web, creating open or incomplete tags.

    
answered by 25.01.2017 в 10:23
0

How about.

It is not necessary to load the static to show an image of an object that you created in the template

The first thing you must ensure is that in your settings you have correctly configured the directory to store the media files and the url to access them

Locate the following settings in the settings.py MEDIA_ROOT MEDIA_URL

Then in your model, make sure you indicate in the field, where you will store it

imagen = models.ImageField(upload_to='path/to/dir/', blank=True, null=True)

If all this is correct and the images are being stored correctly, it would be enough with your template to place

<img src="/media/{{jugador.imagen}}" />

Greetings and I hope my answer will be useful to you

    
answered by 25.01.2017 в 14:00