【问题标题】:Reverse for 'create_order' with no arguments not found. 1 pattern(s) tried: ['create_order/(?P<pk>[^/]+)/$']未找到没有参数的“create_order”反向。尝试了 1 种模式:['create_order/(?P<pk>[^/]+)/$']
【发布时间】:2020-11-24 23:58:32
【问题描述】:

我在使用时遇到此错误

path('create_order/<str:pk>/', views.createOrder, name="create_order"),

但路径为..时没有这样的错误。

path('create_order', views.createOrder, name="create_order"),

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_test>/', views.customer, name="customer"),

    path('create_order/<str:pk>/', views.createOrder, name="create_order"),
    path('update_order/<str:pk>/', views.updateOrder, name="update_order"),
    path('delete_order/<str:pk>/', views.deleteOrder, name="delete_order"),


]

views.py

def createOrder(request, pk):
    OrderFormSet = inlineformset_factory(Customer, Order , fields=('product','status'), extra=9)
    customer = Customer.objects.get(id=pk)
    formset = OrderFormSet(queryset=Order.objects.none(), instance=customer)
    #form = OrderForm(initial={'customer':customer})
    if request.method == 'POST':
        #print('printing post', request.POST)
        formset = OrderFormSet(request.POST, instance=customer)
        if formset.is_valid():
            formset.save()
            return redirect('/')

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

我也尝试过重定向,但问题出在 urls.py 上。

customer.html

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

{% block content %}

<br>
<div class="row">
    <div class="col-md">
        <div class="card card-body">
            <h5>Customer:</h5>
            <hr>
            <a class="btn btn-outline-info btn-sm btn-block" href="">Update Customer</a>
            <a class="btn btn-outline-info  btn-sm btn-block" href="{% url 'create_order' customer.id %}">Place Order</a>
        </div>
    </div>

   <div class="col-md">
        <div class="card card-body">
            <h5>Contact Information</h5>
            <hr>
            <p>Email: {{customer.email}}</p>
            <p>Phone: {{customer.phone}}</p>
        </div>
    </div>

    <div class="col-md">
        <div class="card card-body">
            <h5>Total Order</h5>
            <hr>
            <h1 style="text-align: center;padding: 10px;">{{order_count}}</h1>
        </div>
    </div>
</div>

<br>
<div class="row">
    <div class="col">
        <div class="card card-body">
            <form method="POST">
                <button class="btn btn-primary" type="submit">Search</button>
            </form>
        </div>
    </div>
</div>

<br>
<div class="row">
    <div class="col-md">
        <div class="card card-body">
            <table class="table table-sm">
                <tr>
                    <th>Product</th>
                    <th>Category</th>
                    <th>Date Ordered</th>
                    <th>Status</th>
                    <th>Update</th>
                    <th>Remove</th>
                </tr>

                {% for order in orders %}
                    <tr>
                        <td>{{order.product}}</td>
                        <td>{{order.product.category}}</td>
                        <td>{{order.date_created}}</td>
                        <td>{{order.status}}</td>
                        <td><a  class="btn btn-outline-info btn-md " href="{% url 'update_order' order.id %}">Update</a></td>
                        <td><a class="btn btn-outline-danger btn-md " href="{% url 'delete_order' order.id %}">Delete</a></td>
                    </tr>
                {% endfor %}
            </table>
        </div>
    </div>
</div>

{% endblock %}

models.py

class Customer(models.Model):
    name = models.CharField(max_length=200, null=True, blank=True)
    phone = models.CharField(max_length=200, null=True, blank=True)
    email = models.CharField(max_length=200, null=True, blank=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)

    def __str__(self):
        return self.name

order_form.html

{% extends 'accounts/main.html' %}
{% load static %}
{% block content %}
<div class="row">
    <div class="col-md-6">
        <div class="card card-body">
            <form action="" method="POST">
                {% csrf_token %}
                {{formset.managment_form}} <!-- to remove the managmentForm data missing or has been tempered wiith , error -->
                {% for form in formset %}
                {{formset}}    <!--in context of views.py -->
                <hr>
                {% endfor %}

                <input class="btn btn-outline-success btn-md" type="submit" name="submit">
            </form>
        </div>
    </div>
</div>

{% endblock %}

我已经添加了模板,感谢所有人,但我认为唯一的问题是 urls.py,因为如果我使用

path('create_order/<str:pk>/', views.createOrder, name="create_order"),

而不是

path('create_order', views.createOrder, name="create_order"),

那我就报错了,否则上面的路径就没有这样的错误了。

【问题讨论】:

  • 这很有道理,因为如果它有一个参数,你需要为pk参数指定值,例如{% url 'create_order' pk=42 %}
  • {% url 'create_order ' customer.id %} 已经指定,问题不在 html 文件中,在 urls.py 中,我不知道为什么它显示不一样其他路径的错误,因为它们具有相同的参数
  • 至少还有一个,注意错误中的“with no arguments”部分。
  • 不工作,检查
  • 分享模板?

标签: django django-views django-templates django-urls


【解决方案1】:

我终于得到了错误。 所以,错误就在这里 我们使用的 href id {% url 'create_order' customer.id %}

这是在 customer.html 中,所以 customer.id 将获得 views.customer 提供的值 但如果你在 view.customer 中看到, 上下文= {'客户':客户,'订单':订单,'orders_count':orders_count} 因为我们跟着一个教程视频,那个人做了一些我们没有做的改变,因为它没有在视频中显示。

他所做的改变是 他将“客户”更改为“客户”,现在 customer.html 的上下文很好 因为现在它知道 customer.id 到底是什么

【讨论】:

    【解决方案2】:

    我不知道是什么问题,只是将 GitHub 中的文件 urls.py 替换为相同的上下文,并且没有显示该错误。

    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_test>/', views.customer, name="customer"),
    
        path('create_order/<str:pk>/', views.createOrder, name="create_order"),
        path('update_order/<str:pk>/', views.updateOrder, name="update_order"),
        path('delete_order/<str:pk>/', views.deleteOrder, name="delete_order"),
    
    
    ]
    

    views.py

    from django.forms import inlineformset_factory
    def createOrder(request, pk):
        OrderFormSet = inlineformset_factory(Customer, Order, fields=('product', 'status'), extra=10 )
        customer = Customer.objects.get(id=pk)
        formset = OrderFormSet(queryset=Order.objects.none(),instance=customer)
        #form = OrderForm(initial={'customer':customer})
        if request.method == 'POST':
            #print('Printing POST:', request.POST)
            #form = OrderForm(request.POST)
            formset = OrderFormSet(request.POST, instance=customer)
            if formset.is_valid():
                formset.save()
                return redirect('/')
    
        context = {'form':formset}
        return render(request, 'accounts/order_form.html', context)
    

    order_form.html

    {%  extends 'accounts/main.html' %}
    {% load static %}
    {% block content %}
    
    
    <div class="row">
        <div class="col-md-6">
            <div class="card card-body">
    
                <form action="" method="POST">
                    {% csrf_token %}
                    {{ form.management_form }}
                    {% for field in form %}
                        {{field}}
                        <hr>
                    {% endfor %}
    
                    <input type="submit" name="Submit">
                </form>
    
            </div>
        </div>
    </div>
    
    
    {% endblock %}
    

    再次,我不知道它为什么会显示此错误以及问题出在哪里,但只是用 GitHub 中的相同代码重新生成它并且它工作。如果有人知道它是如何工作的,那将在不久的将来非常有用。

    【讨论】:

      【解决方案3】:

      我知道问题出在哪里

      在dashboard.html中你应该删除包含{% url 'create_order' %}的行

      【讨论】:

        【解决方案4】:

        在仪表板中,您应该删除带有创建订单的行 因为使用了没有 id 的 create_order url 所以有一个错误

        【讨论】:

          猜你喜欢
          • 2019-09-07
          • 1970-01-01
          • 2020-05-13
          • 2020-05-18
          • 2021-08-10
          • 2022-07-12
          • 1970-01-01
          • 2019-02-07
          • 2019-11-18
          相关资源
          最近更新 更多