使用django表单时的NoReverseMatch

时间:2015-08-23 15:58:34

标签: python django

我现在用Django表格挣扎了很长时间。我的表单在不使用Django表单的情况下工作,但使用它似乎更好。我收到一个反向错误,完整错误:

Reverse for 'dish_update' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

我担心我会忽视这里显而易见的事情,但我会非常感激一些帮助。这是我的代码:

forms.py

from django import forms

from .models import Dish


class DishForm(forms.ModelForm):
class Meta:
    model = Dish
    fields = ('dishName', 'dishStars',)

HTML

<h1>{{ dish.dishName }}</h1> 
<h4>{{ dish.dishDescription }}</h4>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<a href="{% url "dish_update" %}"></a>

urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<dish_id>[0-9]+)/$', views.detail, name='detail'),
    url(r'^(?P<dish_id>[0-9]+)/update/$', views.dish_update,        name='dish_update'),
]

views.py

from django.shortcuts import get_object_or_404
from django.shortcuts import render

from .models import Dish
from .forms import DishForm


def index(request):
    dishes = Dish.objects.all()
    context = {'dishes': dishes}
    return render(request, 'bbqcontrol/index.html', context)


def detail(request, dish_id):
    dish = get_object_or_404(Dish, pk=dish_id)
    return render(request, 'bbqcontrol/detail.html', {'dish': dish})


def dish_update(request):
    form = DishForm()
    return render(request, 'bbqcontrol/dish_update.html', {'form': form})

1 个答案:

答案 0 :(得分:0)

您的网址dish_update采用dish_id中不存在的参数{% url "dish_update" %}。这就是反转器失败的原因。

通过将对象的ID作为参数包含来更新您的URL标记:

<a href="{% url "dish_update" dish.id %}"></a>