我有两种看法,
def create_order(request,id):
orders = Order()
orders.restaurant = Restaurant.objects.get(pk=id)
orders.save()
if orders.pk:
....
return orders
第二个视图调用第一个视图,
def show_checkout(request,id):
if order.is_empty(request):
...
if request.method == 'POST':
restaurant = Restaurant.objects.get(pk=id)
form = forms.CheckoutForm(request.POST,instance=restaurant)
if form.is_valid():
order_created = create_order(request,restaurant)
.......
return render_to_response('checkout/checkout.html',context,context_instance=RequestContext(request))
我当我运行这个我得到错误`
show_checkout() 正好接受 2 个参数(1 个给定)` 我的 Traceback,
Traceback:
File "/home/matsinvasion/projects/f4l/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
Exception Type: TypeError at /orders/checkout/
Exception Value: show_checkout() takes exactly 2 arguments (1 given)
我会继续这样做,并建议阅读有关处理争论的好书,这是值得赞赏的
from .views import show_item, show_order,get_category,homepage,show_checkout,reciept
from f4l import settings
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
url(r'^$',homepage,name="homepage"),
url(r'^menu/(\d+)$',get_category, name="f4l_menu"),
url(r'^your_order/$',show_order,name="order_index"),
url(r'^item/(?P<id>\w+)/$',show_item, name="item_order"),
url(r'^checkout/$',show_checkout,name="checkout"),
url(r'^reciept/$',reciept,name="checkout_reciept"),
)
修复 urls.py,NoReverseMatch error
在这个函数中引发,
Reverse for 'checkout' with arguments '()' and keyword arguments '{}' not found.
def get_checkout_url(request):
return urlresolvers.reverse('checkout')