Django:反向'*',参数'()'和关键字参数'{a,b}'未找到。尝试了1种模式:['order / pay /(?P <pk> \\ d +)/ $']

时间:2016-11-08 08:07:18

标签: django django-templates django-views

我要观点。触发客人订单概览的一个。还有一个订单付款,然后应该回到概述:

@login_required
def guest_detail(request, pk):
    guest = get_object_or_404(Guest, pk=pk)
    open_orders = Order.objects.filter(guest = guest, is_paid=False)
    paid_orders = Order.objects.filter(guest = guest, is_paid=True)
    if request.method == "POST":
        form = RegisterGuestForm(request.POST, instance=guest)
        if form.is_valid():
            guest = form.save(commit=False)
            guest.save()
            #post.published_date = timezone.now()
            return redirect('guest_detail', pk=guest.pk)
    else:
            form = RegisterGuestForm(instance=guest)
    context = {'form': form}
    context['open_orders'] = open_orders
    context['paid_orders'] = paid_orders
    return render(request, 'hotel/guest_detail.html', context)

此视图将呈现给此模板:

<div class="col-md-8 col-xs-12">
  <h3>Guest Information:</h3>
  <form method="POST" class="post-form">{% csrf_token %}
    {{ form.as_p }}
    <button type="submit" class="save-guest btn btn-default">Save</button>
  </form>
  <h3>Open orders:</h3>
  <table class="table table-condensed table-guest-order">
    <tr>
      <th>Date</th>
      <th>Amount</th>
      <th>Item</th>
      <th>Price</th>
      <th><th>
    </tr>
   {% for order in open_orders %}
   <tr>
      <td>{{ order.date }}</td>
      <td>{{ order.amount }}</td>
      <td>{{ order.item }}</td>
      <td>{{ order.price }}</td>
      <td><a href="{% url 'pay_order' pk=order.pk order_guest=order.guest.pk %}">pay</a></td>
   </tr>
   {% endfor %}
 </table>
</div>

pay_order链接然后调用以下视图,然后该视图应该将我重定向到我来自的地方:

@login_required
def pay_order(request, *args, **kwargs):
    order = Order.objects.get(pk=kwargs['pk'])
    if order.is_paid is False:
        order.is_paid = True
        order.paid_date = timezone.now()
        order.save()
    return redirect(views.guest_detail, pk=kwargs['order_guest'])

但我收到此错误

  

使用参数'()'和关键字参数反转'pay_order'   '{'order_guest':10,'pk':19}'找不到。尝试过1种模式:   [ '顺序/付费/(ΔP\ d +)/ $']

这是使用的urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^rooms/$', views.ListRoomsView.as_view(), name='rooms'),
    url(r'^service/$', views.service, name='service'),
    url(r'^contact/$', views.contact, name='contact'),
    url(r'^about/$', views.about, name='about'),
    url(r'^guests/$', views.ListGuestsView.as_view(), name='guests'),
    url(r'^guests/edit/(?P<pk>\d+)/$', views.guest_detail, name='guest_detail'),
    url(r'^register_guest/$', views.register_guest, name='register_guest'),
    url(r'^order/edit/(?P<pk>\d+)/$', views.order_detail, name='order'),
    url(r'^order/$', views.register_order, name='new_order'),
    url(r'^order/list/$', views.ListOrdersView.as_view(), name='open_orders'),
    url(r'^order/pay/(?P<pk>\d+)/$', views.pay_order, name='pay_order'),

    ]

当我为已付款的来宾打电话给guest_detail视图时。如果客人没有未付款,一切正常。所以我认为问题是我尝试将guest虚拟机的pk传递给pay_order视图的方式。

1 个答案:

答案 0 :(得分:1)

您正在尝试为&#34; order_guest&#34;传递参数到pay_order视图,但URL不期望该参数,也不是使用它的视图。你根本不应该通过它;从{% url %}标记中的值中取出。

<a href="{% url 'pay_order' pk=order.pk %}">pay</a>