TypeError: context must be a dict rather than str

Question:

I am newbie. I am currently learning django.
I have a page with a meeting of goods:

URLS.PY

urlpatterns = [

    path("productsHTML/<str:uuid>", productHTML, name = "productHTML"),
    path("productsHTML/", productsHTML, name = "productsHTML")
]

VIEW.PY

def productsHTML(request):
    all_products = {"products": Product.objects.all()}
    return render(request, "products.html",all_products) 

def productHTML(request, uuid):
    product = Product.objects.get(id = uuid)
    
    value = [product.name, product.category, product.marka, product.privod, product.loshad_sila, product.box_peredach, product.volume_dvigatel]
    values = {"values": value}

    return render(request, "product.html", uuid, values) 

PRODUCTS.HTML

{% block content %}
<h2>Продукты</h2>
<hr>
    {%if products %}
        {% for product in products %}
                        
            <p><b>Категоия: </b>{{ product.category}}</p>
            <p><b>Название: </b><a href="http://127.0.0.1:8000/productsHTML/{{ product.id }}">{{ product.name}}</a></p>
            <p><b>Цена: Бесплатно</p> 
            <hr> 

        {% endfor %}
            
    {% endif %}
        
{% endblock %}

PRODUCTS.HTML

{% extends 'products.html' %}

{% block title %}Продукт{% endblock %}

{% block content %} 
        {% if values %}
                {% for value in values %}
                    <p><b>Категоия: </b>{{ value.category }}</p>
                    <p><b>Название: </b>{{ value.name }}</p>
                    <p><b>Марка: </b>{{ value.marka }}</p>
                    <p><b>Привод: </b>{{ value.privod }}</p>
                    <p><b>Лошадиные силы: </b>{{ value.loshad_sila }}</p>
                    <p><b>Коробка передач: </b>{{ value.box_peredach }}</p>
                    <p><b>Объём двигателя: </b>{{ value.volume_dvigatel }}</p>
                    <p><b>Цена: Бесплатно</p> 
                
                {% endfor %}

            <button>КУПИТЬ</button>

        {% endif %}     
{% endblock %}

I need to click on the link contained in the product name to be redirected to the product page with the address http://127.0.0.1:8000/productsHTML/ product id.
If needed, I can provide additional information.
Error: TypeError: context must be a dict rather than str.
Thank you in advance!

I’ve been struggling with this for 3 days now. I tried to pass variables to the template in a different way, but to no avail.

Answers:

return render(request, "product.html", uuid, values) 

The problem is that you’re passing uuid as the third argument, but render() expects that the third argument will be a context dictionary.

Why are you even passing uuid to render()? The template does not appear to need it.

Answered By: John Gordon
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.