【问题标题】:Reverse for 'create-ordercontent' with arguments '('',)' not found. 1 pattern(s) tried: ['store/create\\-ordercontent/(?P<order_id>[^/]+)$']未找到带有参数 '('',)' 的 'create-ordercontent' 的反向操作。尝试了 1 种模式:['store/create\\-ordercontent/(?P<order_id>[^/]+)$']
【发布时间】:2021-07-07 06:38:04
【问题描述】:

我是 Django 的新手。我正在尝试将 id 从第一个 url 传递到第二个 ur。

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

urls.py

path('list-ordercontent/<order_id>', OrdercontentListView.as_view(), name='list-ordercontent'),
path('create-ordercontent/<order_id>', create_ordercontent, name='create-ordercontent'),

view.py

class OrdercontentListView(ListView):
    model = Ordercontent
    template_name = 'store/list_ordercontent.html'
    context_object_name = 'ordercontent'

    def get_queryset(self):
        model = Ordercontent
        order = self.request.resolver_match.kwargs['order_id']
        return model.objects.filter(order=order)

在 list-ordercontent.html 中,我尝试使用相同的 order_id 来传递我想要的变量,但它不正确。我想知道如何将 order_id 用于另一个 url,在我的程序中它是用于创建的 url。

<div class="breadcrumbs">
<div class="breadcrumbs-inner">
    <div class="row m-0">
        <div class="col-sm-4">
            <div class="page-header float-left">
                <div class="page-title">
                    <h1>Dashboard</h1>
                </div>
            </div>
        </div>
        <div class="col-sm-8">
            <div class="page-header float-right">
                <div class="page-title">
                    <ol class="breadcrumb text-right">
                        <li><a href="#">Dashboard</a></li>
                        <li><a href="#">Order</a></li>
                        <li class="active">
                            <a>{{ ordercontent.order.id }}</a>
                            <a>Content</a>
                        </li>
                    </ol>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="row">
<div class="col-xl-12">
    <div class="card">
        <div class="card-body">
            <div class="box-title float-left">
                <h4>Order Content</h4>
            </div>
            <div class="box-title float-right">
                <a href="{% url 'create-ordercontent' order_id %}">Add Product</a>
            </div>
        </div>
        <div class="card-body--">
            <div class="table-stats order-table ov-h">
                <table class="table ">
                    <thead>
                        <tr>
                            <th class="serial">#</th>
                            <th>Name</th>
                            <th>Type</th>
                            <th>Supplier</th>
                            <th>Amount</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% if ordercontent %}
                        {% for ordercontent in ordercontent %}
                        <tr>
                            <td class="serial">{{ ordercontent.order.id }}</td>
                            <td>{{ ordercontent.product.name }}</td>
                            <td>{{ ordercontent.product.ptype }}</td>
                            <td>{{ ordercontent.product.supplier }}</td>
                            <td>{{ ordercontent.amount }}</td>
                        </tr>
                        {% endfor %}
                        {% else %}
                            <tr><td>No Content</td></tr>
                        {% endif %}
                    </tbody>
                </table>
            </div> <!-- /.table-stats -->
        </div>
    </div> <!-- /.card -->
</div>  <!-- /.col-lg-8 -->

【问题讨论】:

  • 你遇到什么错误??你能补充更多信息吗?
  • 当我尝试打开网址时,创建订单内容。未找到参数“(”,)“的“创建订单内容”的错误是反向的。尝试了 1 种模式:['store/create\\-ordercontent/(?P[^/]+)$']
  • 显示你的模板完整代码添加产品这个
  • 是的。我展示了我的完整代码。
  • 您从哪里获取了这个 order_id 变量

标签: django django-templates django-urls


【解决方案1】:

ListView 不会将视图接收到的关键字参数添加到上下文中,如果您希望它们在上下文中,您应该通过覆盖 get_context_data 自己添加它们:

class OrdercontentListView(ListView):
    model = Ordercontent
    template_name = 'store/list_ordercontent.html'
    context_object_name = 'ordercontent'

    def get_queryset(self):
        model = Ordercontent
        order = self.kwargs['order_id'] # No need to access `request.resolver_match.kwargs` they are present in `self.kwargs`
        return model.objects.filter(order=order)
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['order_id'] = self.kwargs['order_id']
        return context

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2020-11-05
    • 2018-11-21
    • 1970-01-01
    • 2019-04-09
    相关资源
    最近更新 更多