-2

I'm trying to display a photo on a page.

My code:

# views.py

def productHTML(request, uuid):
    product = Product.objects.get(id = uuid)
    path = product.category.path_to_foto
   
    slovar = {"product": product, "category": path}
    return render(request, "product.html", slovar) 

productHTML.html:

{% extends 'products.html' %}
{% load static%}
{% block title %}Продукт{% endblock %}

{% block page_content %} 
        {% if product %}
                <p>Путь до фото: {{path}} </p>
                <img src="{% static   {{ path }}  %}" alt="product picture" width="540" height="300">   
                <p><b>Категоия: </b>{{ product.category }} </p>
                <p><b>Название: </b>{{ product.name }}</p>
                <p><b>Марка: </b>{{ product.marka }}</p>
                <p><b>Привод: </b>{{ product.privod }}</p>
                <p><b>Лошадиные силы: </b>{{ product.loshad_sila }}</p>
                <p><b>Коробка передач: </b>{{ product.box_peredach }}</p>
                <p><b>Объём двигателя: </b>{{ product.volume_dvigatel }}
                <p><b>Цена: Бесплатно</p>
                
            <button>КУПИТЬ</button> 
        {% endif %}     
{% endblock %}

Through Product.objects.get(id = uuid) I get a specific product. Through product.category.path_to_foto I get the path to the photo of this product. After that I'm trying to put this path into a variable and display it on the html page, but I get an error:

django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '{{' from '{{'

All product photos are stored in static/media

If you need any details, I am ready to provide them.

I tried to change the path to the photo. For example, I tried to replace media/<file> with static/media/<file> to no avail. I also tried to implement the variable differently, without success.

1 Answers1

0

I'm not sure if you can use variables inside of the static tag, but please correct me if I'm wrong.

This is a route you could go:

{% load static %}
<img src="{% get_static_prefix %}{{ path }}" alt="image">   

Docs: https://docs.djangoproject.com/en/4.2/ref/templates/builtins/#get-static-prefix

Nealium
  • 2,025
  • 1
  • 7
  • 9
  • the error is gone but the photo does not show – Глеб Гудалин Mar 10 '23 at 17:13
  • If you are *uploading* a photo you have to use the `media` root. Check out this question: https://stackoverflow.com/questions/5517950/django-media-url-and-media-root -other wise it should work, make sure you `collectstatic` and try manually opening up the URL – Nealium Mar 12 '23 at 16:46