Answer the question
In order to leave comments, you need to log in
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 = 'Ингредиент'
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("Здесь будет выведен список коктейлей.")
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),
]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %} {% endblock title %}</title>
</head>
{% block content %}
{% endblock content %}
</body>
</html>
{% 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 %}
Answer the question
In order to leave comments, you need to log in
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),
]
имя%20коктейля
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question