Django Ошибка: Обратное для 'delete_ingredient' не найдено. 'delete_ingredient' не является допустимой функцией представления или именем шаблона
Буду благодарен за любую помощь, которую вы можете оказать, чтобы я смог преодолеть этот блок. Спасибо!
Код ломается, когда я модифицирую код, который раньше работал, чтобы добавить функциональность Delete. Когда я запускаю код без модифицированного urls.py, я получаю ошибку.
Ошибка:
> NoReverseMatch at /ingredients/
Reverse for 'delete_ingredient' not found. 'delete' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/ingredients/
Django Version: 4.1.1
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'delete_ingredient' not found. 'delete' is not a valid view function or pattern name.
Exception Location: C:\Users\Dropbox\Python Projects\Django Projects\djangodelights\djangodelights_env\lib\site-packages\django\urls\resolvers.py, line 828, in _reverse_with_prefix
Raised during: inventory.views.IngredientsView
Python Executable: C:\Users\Dropbox\Python Projects\Django Projects\djangodelights\djangodelights_env\Scripts\python.exe
Python Version: 3.9.5
Python Path:
['C:\\Users\\Python Projects\\Django '
'Projects\\bwa-django-final-project-main\\bwa-django-final-project',
'C:\\Users\\Python Projects\\Django '
'Projects\\djangodelights\\djangodelights_env\\Scripts\\python39.zip',
'd:\\users\\miniconda3\\DLLs',
'd:\\users\\miniconda3\\lib',
'd:\\users\\miniconda3',
'C:\\Users\\Python Projects\\Django '
'Projects\\djangodelights\\djangodelights_env',
'C:\\Users\\Python Projects\\Django '
'Projects\\djangodelights\\djangodelights_env\\lib\\site-packages']
Server time: Wed, 19 Oct 2022 19:44:34 +0000
Error during template rendering
In template C:\Users\Dropbox\Python Projects\Django Projects\bwa-django-final-project-main\bwa-django-final-project\inventory\templates\inventory\ingredients_list.html, error at line 30
Reverse for 'delete_ingredient' not found. 'delete' is not a valid view function or pattern name.
20 </thead>
21 <tbody>
22 {% for ingredient in object_list %}
23 <tr>
24 <td>
25 <a href="{% url "update_ingredient" ingredient.id %}">{{ ingredient.name }}</a>
26 </td>
27 <td>{{ ingredient.quantity }}</td>
28 <td>{{ ingredient.unit }}</td>
29 <td class="price">${{ ingredient.price_per_unit|stringformat:".2f" }}</td>
30 <td><a href="{% url "delete_ingredient" ingredient.id %}"
31 style="text-decoration: none; color: inherit;">❌</a></td>
32 </tr>
33 {% endfor %}
34 </tbody>
35 </table>
36 <a href="{% url "create_ingredient" %}" style="display:
37 inherit; text-align: center; border-radius: 15px; text-decoration: none;
38 color: inherit; padding: 4px; background-color: lightgreen;">➕</a>
39 {% endblock %}
Оригинальный код
ingredients_list.html
{% extends 'base.html' %}
{% load static %}
{% block title %}Django Delights{% endblock %}
{% block head %}
{% endblock %}
{% block content %}
<h2>Ingredients</h2>
<a href="{% url 'add_ingredient' %} ">Add New Ingredient to Inventory</a>
<hr/>
<table class="inventory-table">
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Unit</th>
<th>Unit Price</th>
</tr>
</thead>
<tbody>
{% for ingredient in object_list %}
<tr>
<td>
<a href="{% url "update_ingredient" ingredient.id %}">{{ ingredient.name }}</a>
</td>
<td>{{ ingredient.quantity }}</td>
<td>{{ ingredient.unit }}</td>
<td class="price">${{ ingredient.price_per_unit|stringformat:".2f" }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path("logout/", views.log_out, name="logout"),
path('accounts/login/', auth_views.LoginView.as_view(), name="login"),
path('', views.HomeView.as_view(), name='home'),
path('ingredients/', views.IngredientsView.as_view(), name="ingredients"),
path('ingredients/new', views.NewIngredientView.as_view(), name="add_ingredient"),
path('ingredients/<slug:pk>/update', views.UpdateIngredientView.as_view(), name="update_ingredient"),
path('menu/', views.MenuView.as_view(), name="menu"),
path('menu/new', views.NewMenuItemView.as_view(), name="add_menu_item"),
path('reciperequirement/new', views.NewRecipeRequirementView.as_view(), name="add_recipe_requirement"),
path('purchases/', views.PurchasesView.as_view(), name="purchases"),
path('purchases/new', views.NewPurchaseView.as_view(), name="add_purchase"),
path('reports', views.ReportView.as_view(), name="reports")
]
Модифицированный код
ingredients_list.html
{% extends 'base.html' %}
{% load static %}
{% block title %}Django Delights{% endblock %}
{% block head %}
{% endblock %}
{% block content %}
<h2>Ingredients</h2>
<!-- <a href="{% url 'add_ingredient' %} ">Add New Ingredient to
Inventory</a> -->
<hr/>
<table class="inventory-table">
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Unit</th>
<th>Unit Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for ingredient in object_list %}
<tr>
<td>
<a href="{% url "update_ingredient" ingredient.id %}">{{ ingredient.name }}</a>
</td>
<td>{{ ingredient.quantity }}</td>
<td>{{ ingredient.unit }}</td>
<td class="price">${{ ingredient.price_per_unit|stringformat:".2f" }}</td>
<td><a href="{% url "delete_ingredient" ingredient.id %}"
style="text-decoration: none; color: inherit;">❌</a></td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{% url "add_ingredient" %}" style="display:
inherit; text-align: center; border-radius: 15px; text-decoration: none;
color: inherit; padding: 4px; background-color: lightgreen;">➕</a>
{% endblock %}
urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path("logout/", views.log_out, name="logout"),
path('accounts/login/', auth_views.LoginView.as_view(), name="login"),
path('', views.HomeView.as_view(), name='home'),
path('ingredients/', views.IngredientsView.as_view(), name="ingredients"),
path('ingredients/new', views.NewIngredientView.as_view(), name="add_ingredient"),
path('ingredients/<slug:pk>/update', views.UpdateIngredientView.as_view(), name="update_ingredient"),
path('ingredients/<id>/delete', views.DeleteIngredientView.as_view(),
name="delete_ingredient"),
path('menu/', views.MenuView.as_view(), name="menu"),
path('menu/new', views.NewMenuItemView.as_view(), name="add_menu_item"),
path('reciperequirement/new', views.NewRecipeRequirementView.as_view(), name="add_recipe_requirement"),
path('purchases/', views.PurchasesView.as_view(), name="purchases"),
path('purchases/new', views.NewPurchaseView.as_view(), name="add_purchase"),
path('reports', views.ReportView.as_view(), name="reports")
]
Когда я запускаю его с модифицированным urls.py, я получаю:
Хммм... не могу попасть на эту страницу
127.0.0.1 отказано в подключении.
Попробуйте:
- Поиск в Интернете по запросу 127 0 0 1
- Проверка соединения
- Проверка прокси и брандмауэра
ERR_CONNECTION_REFUSED