我遇到了NoReverseMatch错误
Reverse for 'item_order' with arguments '()' and keyword arguments '{}' not found.
我有这个模板运行时调用的视图
def show_item(request,id):
# need to evaluate the HTTP method
if request.method == 'POST':
a = Item.objects.get(pk=id)
form = forms.PartialOrderItemForm(request.POST,instance=a)
# check validation of posted data
if form.is_valid():
order.add_to_order(request)
# if test cookie worked, get rid of it
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
url =urlresolvers.reverse('order_index')
# redirect to order page
return HttpResponseRedirect(url)
else:
# it's a GET, create the unbound from. Note request as a Kwarg
form = forms.PartialOrderItemForm(request.GET)
# set the test cookie on our first GET request
request.session.set_test_cookie()
context={
# 'categories':categories,
'form':form,
# 'menu':menu,
}
return render_to_response('item.html',context,context_instance=RequestContext(request))
当此视图运行时,它会在某个时刻调用此函数
def add_to_order(request):
# import pdb; pdb.set_trace()
postdata = request.POST.copy()
# get item slug from post data, return blank if empty
# item_slug = postdata.get('item_slug','')
#get quantity added, return 0 if empty
quantity = postdata.get('quantity',0)
# fetch the item or return missing page error_message
i = get_object_or_404(Item)
# get items in order
order_items = get_order_items(request)
item_in_orders = False
# check to see if item is already in order
for order_item in order_items:
if order_item.item.id == i.id:
#update the quantity if found
order_item.augment_quantity(quantity)
item_in_orders = True
if not item_in_orders:
# creat and save a new order item
oi = OrderItem()
oi.order_id = _order_id(request)
oi.quantity = quantity
oi.item = i
oi.save()
urls.py,
from .views import show_item, show_order,get_category
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
url(r'^$',get_category, name="item_order_home"),
url(r'^your_order/$',show_order,name="order_index"),
url(r'^item/(?P<id>\w+)/$', show_item, name = "item_order"),
# url(r'^menu/$',get_category,name = "f4l_menu"),
)
在模板中,这是正在做的事情
{% for category in categories %}
<h5> {{ category.title }} </h5>
{% for item in category.item.all %}
<li><a href="{% url item_order %}">{{item.name}}<span> {{item.price}}frw</span></li><a/>
{% endfor %}
{% endfor %}
我想要做的是,他们是几件物品,当我点击一件时,它会将我重定向到一个表格,我提交我想要购买的物品的数量,当我提交表格时我被重定向到订单页面。 但是我遇到了这个错误。
答案 0 :(得分:4)
您需要传递id值,因为您在网址
中指定了它{% url item_order item.id %}