Hello guys, I just started into Django + Pythin and I’m trying to create a simple website that gets registered users and show them(fields like: email,name,surename,image(avatar))
But for some reason I’m not being able to add uploaded pictures…
[img not showing][1]
[1]: https://i.stack.imgur.com/DuN2R.png
urlpatterns = [
path('admin/', admin.site.urls),
url(r'',include("func.urls")), ]
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
**Models**
class Categoria(models.Model):
nome = models.CharField(max_length=100)
dt_criacao = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.nome
class Transacao(models.Model):
data = models.DateTimeField(auto_now=True)
email = models.CharField(max_length=20)
nome = models.CharField(max_length=20)
sobrenome = models.CharField(max_length=20)
investimentos = models.ForeignKey(Categoria, on_delete=models.CASCADE,blank=True)
#observacoes = models.TextField(null=True,blank=True)
imagem = models.ImageField(default='func/images/default.png',upload_to='images',null=True, blank=True)
def __str__(self):
return self.email
**Settings**
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'func/static/func')
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
**HTML**
<div class="container-fluid teste">
<div class="row">
<div class="col-lg-12 col-12 text-light bg-transparent">
<h1 class="nossos font-weight-bold ">Visualização </h1>
<h1 class=" font-weight-bold ">TESTE </h1>
</div>
</div>
{%for transacao in transacoes %} {{transacao.image}} {%endfor%} {%for transacao in transacoes %}
<!-- item 1 -->
<div class="col-lg-4 col-12">
<div class="card bg-transparent">
<a data-toggle="modal" data-target="#modal-default{{transacao.id}}">
<img src="{% if transacao.image %}
{{transacao.imagem.url}}
{% endif %}" class="rounded-circle img-fluid max-width:33%" style="padding: 5%;">
</a>
<!-- modal -->
<div id="modal-default{{transacao.id}}" class="modal fade">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<!-- título -->
<div class="modal-header">
<h4>{{transacao.nome}}</h4>
<button class="close" data-dismiss="modal" arial-label="Fechar">
<span aria-hidden="true">×</span>
</button>
</div>
<!-- conteudo -->
<div class="modal-body">
<div class="row">
<div class="col-lg-6 col-12">
<img src="{{transacao.imagem}}" class="rounded img-fluid img-lg">
</div>
<div class="col-lg-6 col-12">
<p class="text-justify font-weight-bold modaltext">
{{transacao.nome}} <br>{{transacao.email}} <br>{{transacao.investimento}}
</p>
</div>
</div>
</div>
<!-- rodapé-->
<div class="modal-footer">
<button class="btn btn-dark btn-block" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
</div>
</div>
{%endfor%}
</div>
I tried
{% if transacao.image %}
{{transacao.imagem.url}}
{% endif %}
And
{{transacao.imagem}}