M
M
mezigar2022-04-07 15:05:08
Python
mezigar, 2022-04-07 15:05:08

Why is the data not showing?

There are models:

from django.db import models


class Cocktail(models.Model):
    title = models.CharField(max_length=50, db_index=True, verbose_name='Название')
    ingredients = models.ManyToManyField('Ingredient', verbose_name='Ингредиент')

    # Stars or rating = Many to One

    def __str__(self):
        return f"{self.title}: {self.id} "

    class Meta:
        verbose_name_plural = 'Коктейли'
        verbose_name = 'Коктейль'


class Ingredient(models.Model):
    title = models.CharField(max_length=30, db_index=True, verbose_name='Название')

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = 'Ингредиенты'
        verbose_name = 'Ингредиент'

There are views:
from django.http import HttpResponse
from django.shortcuts import render

from .models import Cocktail


def by_cocktail_id(request, cocktail_id):
    cocktail = Cocktail.objects.all().filter(id=cocktail_id)
    # cocktail = Cocktail.objects.get(id=cocktail_id)
    context = {'cocktail': cocktail}
    return render(request, 'by_cocktail_id.html', context)


def by_cocktail_name(request, cocktail_name):
    cocktail = Cocktail.objects.all().filter(title=cocktail_name)
    context = {'cocktail': cocktail}
    return render(request, 'by_cocktail_name.html', context)


def index(request):
    return HttpResponse("Здесь будет выведен список коктейлей.")

URL file:
from django.urls import path

from .views import index, by_cocktail_id, by_cocktail_name

urlpatterns = [
    path('<int:cocktail_id>/', by_cocktail_id),
    path('<str:cocktail_name>/', by_cocktail_name),
    path('', index),
]


Basic html template:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{% block title %} {% endblock title %}</title>
    </head>
    {% block content %}

    {% endblock content %}
    </body>
</html>

And the html file:
{% extends "base.html" %}

{% block title %}
    {{ cocktail.title }}
{% endblock %}

{% block content %}
    <div>
        <h3>Коктейль номер {{ cocktail.id }}</h3>
        <lu>

            {% for ingredient in cocktail.ingredients %}
            <li>
                <p>{{ ingredient }}</p>
            </li>
            {% endfor %}

        </lu>
    </div>
{% endblock %}

As a result, the data is not displayed, an empty template. In devtools, there is no data in html, where does the data go?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2022-04-07
@AlexNest

What address exactly? The problem, perhaps, is that you have urls that are equivalent, from the point of view of the parser:

urlpatterns = [
    path('<int:cocktail_id>/', by_cocktail_id),
    path('<str:cocktail_name>/', by_cocktail_name),
    path('', index),


]

And let's say there is a url like: mysite.ru/1/
And how should django understand this? 1 as a string, 1 as a number?
To begin with, isolate the routes more specifically. Next - check each route separately. There are at least a few "points of interest": What data is coming in? Does django find cocktails in the database (print to console)? Is the data being passed to the template correctly?
And yes, using the name in its pure form as a route is a dubious practice. At a minimum, if there are spaces in the name, they will be replaced by %20 And then something like this will come as a parameter:
имя%20коктейля
Use slug

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question