G
G
gromyko212020-04-16 10:18:24
Django
gromyko21, 2020-04-16 10:18:24

How to display model variables in Django template?

Good day to all. I'll get straight to the point - I just can't display the variables from the model (which are already created in the admin panel) in the template. I have been sitting with this moment for 3 days, reviewed a bunch of guides, including the documentation, and for some reason nothing comes out.
view:

from django.shortcuts import render, get_object_or_404
from .models import Kitchen

def kuhni(request):
    return render(request,'kuhni/kuhni.html')
def direct_kuhni(request):
    return render(request,'kuhni/direct_kuhni.html')
def direct_name(request, id):
    direct_product = get_object_or_404(Kitchen,
                                    id=id)
    return render(request,'kuhni/unique_kuhni.html', {'direct_product' : direct_product})

models:
from django.db import models

class Kitchen(models.Model):
    nameKitchen = models.CharField(max_length=30, name='Название')
    top_facade = models.CharField(max_length=30, name='Верхние фасады', null=True, blank=True)
#Ниже остальное

    def get_absolute_url(self):
        return "/kuhni/direct_kuhni/%i/" % self.id

url of this application, there are no problems, the url is created correctly:
from django.urls import path
from . import views

urlpatterns = [
    path('', views.kuhni, name='kuhni'),
    path('direct_kuhni/', views.direct_kuhni,name='direct_kuhni'),
    path('direct_kuhni/<int:id>/', views.direct_name),
]

And the template itself:
{% extends 'base/base.html' %}
{% load static %}

{% block title %} {{ nameKitchen }} {% endblock %}
{% block content %}
    <a>{{ nameKitchen }}</a>
    <p>{{ top_facade }}</p>
{% endblock %}


I tried many methods (mostly changed the view).
There are no problems with the models - they are displayed in the admin panel as they should, and when you click on "View on the site", an empty template opens (variables are not shown in any way).
Thank you all in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-04-16
@bacon

Hey, what were you looking at.{{ direct_product.nameKitchen }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question