我创建了一个[
{
"pK_ID": 1,
"name_EN": "John",
"count": 0,
"phone": "52525",
"image": "/john.png"
}]
函数来删除膳食对象。
当我运行链接restaurant_delete_meal
时,该功能可以工作并删除该对象。但是当我尝试在编辑餐页面中添加一个带有删除餐功能的网址的按钮时,我尝试打开编辑餐页面获取此错误
restaurant/meal/id/delete
我导入了所有视图。编辑用餐页面,我尝试设置删除按钮:
NoReverseMatch at /restaurant/meal/9/edit/
Reverse for 'restaurant-delete-meal' with arguments '('',)' not found.
1 pattern(s) tried: ['restaurant/meal/(?P<meal_id>\\d+)/delete/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/restaurant/meal/9/edit/
Django Version: 1.11
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'restaurant-delete-meal' with arguments '('',)' not found.
1 pattern(s) tried: ['restaurant/meal/(?P<meal_id>\\d+)/delete/$']
Exception Location:
/Users/mickael/Desktop/myvirtualenv/foodtasker/lib/python3.6/site-
packages/django/urls/resolvers.py in _reverse_with_prefix, line 497
Python Executable:
/Users/mickael/Desktop/myvirtualenv/foodtasker/bin/python
Python Version: 3.6.5
Python Path:
['/Users/mickael/Desktop/foodtasker',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-
dynload',
'/Users/mickael/Desktop/myvirtualenv/foodtasker/lib/python3.6/site-
packages']
Server time: Wed, 16 May 2018 11:16:53 +0000
Error during template rendering
In template
/Users/mickael/Desktop/foodtasker/foodtaskerapp/templates/base.html,
error at line 25
Reverse for 'restaurant-delete-meal' with arguments '('',)' not found.
1 pattern(s) tried: ['restaurant/meal/(?P<meal_id>\\d+)/delete/$']
Views.py
[views.py][1]
[urls.py][2]
[edit-meal.html][3]
edit_meal.html当我尝试设置按钮
时 def restaurant_delete_meal(request, meal_id):
instance = get_object_or_404(Meal, id=meal_id)
instance.delete()
messages.success(request, "DELETED")
return redirect(restaurant_meal)
urls.py
{% extends 'restaurant/base.html' %}
{% load bootstrap3 %}
{% block page %}
<div class="col-lg-offset-2 col-lg-8">
<div class="panel">
<div class="panel-heading bg-blue">
<h4 class="panel-title text-center text-white">
Edit Meal
</h4>
</div>
<div class="panel-body">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form form %}
<button type="submit" class="btn btn-pink">Update</button>
</form>
<button type="button" class="btn btn-pink" id="right-panel-link" href="{% url 'restaurant-delete-meal' meal.id %}">Add</button>
</div>
</div>
</div>
{% endblock %}