Search code examples
pythondjangodjango-viewsbackend

Why is nothing displayed on the page in Django?


Here's a question, I have products on a page and each product has its own slug. This slug redirects to the personal page of the product, which displays all the necessary information about the product (name, description, images, etc). But for some reason the page is just empty, i.e. what is written in the html file is not displayed and I don't understand why. The redirect is error free and the product slug appears in the url, but there is no product information.

P.s in the view, I use print to see in the terminal if I actually go to the product page and get the product name. And yes, I do get it in the terminal

views.py

def product_detail(request, post):
    product = get_object_or_404(ProductModel, slug=post)
    print(product.title)
    return render(request, 'products/detail-products.html', {
        'product': product,
    })

urls.py

from django.urls import path

from products import views

app_name = 'products'

urlpatterns = [
    path('', views.products, name='products'),
    path('product/<slug:post>/', views.product_detail, name='product_detail'),
]

product.html - There's a button here that redirects to the personal information page.

<div class="button__more">
      <a href="{% url 'products:product_detail' product.slug %}"><button>Подробнее</button></a>
</div>

product-detail.html

{% extends 'main/base.html' %}
{% block title %}Detail {{ product.title }}{% endblock title %}

{% block content %}
  <style>
    .product__detail__photo img {
      width: 60%;
      height: auto;
      background-size: cover;
      object-fit: cover;
      border-radius: 5px;
    }
  </style>

  <div class="product__detail">
    <div class="product__image">
      {% if product.product_images %}
        <img src="{{ product.product_images.url }}" alt="product image">
      {% else %}
        <p>No image available</p>
      {% endif %}
    </div> 
    <div class="photo__detail__info">
      <div class="photo__detail__info__title">
        <h1>{{ product.title }}</h1>
      </div>
      <div class="photo__detail__info__description">
        <h1>{{ product.full_description }}</h1>
      </div>
    </div>
  </div>
{% endblock content %}

base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}{% endblock title %}</title>
</head>
<body>
    {% block content %}
    {% endblock content %}
</body>
</html>

I've tried rewriting the view and using different ways to redirect by slg, I've tried doing redirection by id even though I don't need it. I even made it possible to output the product name to the console if you open the product's personal page and everything outputs fine, but there is nothing on the page itself that should be in the html file.

I want that when you go to the detailed page of the product on its slug I would display all the necessary information about this product recorded in the html file, but this does not happen for some reason and I do not understand why so. There is no error in the terminal.


Solution

  • It seems that you are passing the 'product' context variable to the detail-products.html template instead of product-detail.html. Try fixing that. If it doesn't work, try passing another context variable to your render function, like 'test': 'Test message', to check if the template is being populated correctly with test data.