【问题标题】:Reverse for 'update_order' with arguments '('',)' not found. 1 pattern(s) tried: ['update_order\\/(?P<pk>[^/]+)\\/$']未找到带有参数 '('',)' 的 'update_order' 的反向操作。尝试了 1 种模式:['update_order\\/(?P<pk>[^/]+)\\/$']
【发布时间】:2020-03-03 22:23:22
【问题描述】:

我猜 updateorder 函数有一些错误,但我找不到它。当我单击 dashboard.html 中的更新按钮时,它会给出一个错误提示

未找到带有参数“('',)' 的“update_order”的反向操作。尝试了 1 种模式:['update_order\/(?P[^/]+)\/$']

views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse
from accounts.models import *
from accounts.forms import OrderForm

# Create your views here.
def home(request):
    customers = Customer.objects.all()
    orders = Order.objects.all()
    total_customers = customers.count()
    total_orders = orders.count()
    pending = orders.filter(status='Pending').count()
    delivered = orders.filter(status='Delivered').count()
    context = {'customers':customers, 'orders':orders, 'pending':pending, 'delivered':delivered, 'total_customers':total_customers,
                    'total_orders':total_orders}
    return render(request, 'accounts/dashboard.html', context)

def products(request):
    products = Product.objects.all()
    return render(request, 'accounts/products.html', {'products':products})

def customer(request, pk):
    customers = Customer.objects.get(id=pk)
    orders = Order.objects.all()
    orders_counts = orders.count()
    context = {'customers':customers, 'orders':orders, 'orders_counts':orders_counts}
    return render(request, 'accounts/customer.html', context)


def createorder(request):

    form = OrderForm
    if request.method=='POST':
        form = OrderForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/')

    context = {'form':form}
    return render(request, 'accounts/order_form.html', context)


def updateorder(request, pk):
    orders = Order.objects.get(id = pk)
    form = OrderForm(instance = orders)
    context = {'form':form}
    return render(request, 'accounts/order_form.html', context)

> Blockquote

urls.py

from django.urls import path
from . import views
urlpatterns = [

    path('', views.home, name = 'home'),
    path('products/', views.products, name = 'products'),
    path('customer/<str:pk>/', views.customer, name = 'customer'),
    path('create_order/', views.createorder, name = 'create_order'),
    path('update_order/<str:pk>/', views.updateorder, name = 'update_order')

]

dashboard.html

{% extends 'accounts/main.html' %}
{% block content %}
{% include 'accounts/status.html' %}

<br>
<div class="row">
  <div class="col-md-5">
    <h5>CUSTOMERS:</h5>
    <hr>
    <div class="card card-body">
      <a class="btn btn-primary btn-sm btn-block" href="">CREATE CUSTOMERS</a>
      <table class = "table table-sm">
        <tr>
          <th>Customers</th>
          <th>Phone</th>
        </tr>

        {% for i in customers %}
          <tr>
            <!-- <td><a href="{% url 'customer' i.id %}">View</a></td> -->
            <td>{{i.name}}</td>
            <td>{{i.phone}}</td>
            <td><a class="btn btn-info" href="{% url 'customer' i.id %}">View</a></td>
          </tr>
        {% endfor %}
      </table>
    </div>
  </div>

  <div class="col-md-7">
    <h5>LAST 5 ORDERS</h5>
    <hr>
    <div class="card card-body">
      <a class="btn btn-primary  btn-sm btn-block" href="{% url 'create_order' %}">CREATE ORDERS</a>
      <table class = "table table-sm">
        <tr>
          <th>Product</th>
          <th>Date Ordered</th>
          <th>Status</th>
          <th>Update</th>
          <th>Remove</th>
        </tr>

        {% for i in orders %}
          <tr>
            <td>{{i.product}}</td>
            <td>{{i.date_created}}</td>
            <td>{{i.status}}</td>
            <td><a class="btn btn-info" href="{% url 'update_order' Order.id %}">Update</a></td>
            <td><a class="btn btn-danger" href="">Delete</a></td>
          </tr>
        {% endfor %}

      </table>
    </div>
  </div>
</div>
{% endblock %}

forms.py

from django.forms import ModelForm
from accounts.models import Order

class OrderForm(ModelForm):
    class Meta:
        model = Order
        fields = '__all__'

order_forms.html

{% extends 'accounts/main.html' %}
{% load static %}

{% block content %}
<form  method="post">
  {% csrf_token %}
  {{form}}
  <input type="submit" name="Submit">
</form>
{% endblock %}

【问题讨论】:

标签: django django-templates


【解决方案1】:
{% for i in orders %}
    ...
    {% url 'update_order' Order.id %}

i,不是Order,所以改成

    {% url 'update_order' i.id %}

【讨论】:

    【解决方案2】:

    对于,你不能使用这样的path(..)s,在这种情况下你需要写一个正则表达式,比如:

        <b>url</b>(<b>r</b>'<b>^</b>complete/<b>(?P</b>&lt;todo_id&gt;<b>[0-9]+)</b><b>$</b>', views.completeTodo, name='complete'),

    如果您使用的是,您可能希望像您一样使用path(..)

    我相信这可能与您设置正则表达式的方式有关。您的模型中的 Order 对象是什么数据类型,我假设它们是字符串,因为这是您在 url 中定义的类型。如果他们按照你所做的那样做,否则如果它的字符串那么做下面的事情

    对于 url,而不是这个:

    url('update_order/&lt;todo_id&gt;', , views.updateorder, name = 'update_order'),

    试试这个:

    url(r'^update_order/(?P<todo_id>\d+)$', , views.updateorder, name = 'update_order'),
    

    或者如果你想使用 [path]

    path('update_order/<int:todo_id>', views.updateorder, name = 'update_order'),
    

    【讨论】:

      猜你喜欢
      • 2022-07-12
      • 2022-01-13
      • 2020-11-24
      • 2021-06-13
      • 2020-02-06
      • 2022-01-17
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多