找不到关键字参数“'client_id': 3”的“all_clients”反向。尝试了 1 种模式:['clients/all_clients/$']

Posted

技术标签:

【中文标题】找不到关键字参数“\'client_id\': 3”的“all_clients”反向。尝试了 1 种模式:[\'clients/all_clients/$\']【英文标题】:Reverse for 'all_clients' with keyword arguments ''client_id': 3' not found. 1 pattern(s) tried: ['clients/all_clients/$']找不到关键字参数“'client_id': 3”的“all_clients”反向。尝试了 1 种模式:['clients/all_clients/$'] 【发布时间】:2021-12-09 06:29:48 【问题描述】:

我是 Django 新手,在我的项目中实现编辑模板时遇到了麻烦。我遇到以下错误:

未找到带有关键字参数“'client_id': 3”的“all_clients”的反向操作。尝试了 1 种模式:['clients/all_clients/$']

我在网站上查看过类似的事件,例如 Reverse for 'plan_edit' with keyword arguments

但我无法确定问题所在。我相信当我向 all_clients.html 模板添加超链接时会出现问题。此外,将加载 /clients/edit_client/?/ 的模板页面,但是在使用保存更改按钮提交后,NoReserse Match 错误会重新出现,因为它尝试加载 clients/all_clients 页面。

见下面的代码:

models.py

from django.db import models

# Create your models here. 
class Client(models.Model):
    #A client is composed of the company general info
    text = models.CharField('Company Name',default = 'Company Name', max_length = 200)
    phone_num = models.CharField('Phone Number', default = '000-000-000', max_length = 12)
    ceo_name = models.CharField ('CEO', max_length = 50)
    num_employees = models.IntegerField('Number of Employees', default = 0)
    maintenance_schedule = models.CharField('maintenance schedule', max_length = 100)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        """Return a string representation of the model."""
        return self.text

urls.py

"""Defines URL patterns for clients."""

from django.urls import path from django.conf.urls import url

from .import views

app_name = 'clients' urlpatterns = [
    #Company Page
    path('index/', views.index, name = 'index'),
    
    #Page for listing all clients
    path('all_clients/', views.all_clients, name = 'all_clients'),

    #Page for adding a new client
    path('all_clients/<int:client_id>/', views.add_client, name = 'add_client'),

    #Page for adding a new client office using a form
    path('new_office/', views.new_office, name = 'new_office'),

    #Page for a company to edit their entry.
    path('edit_clients/<int:client_id>/', views.edit_client, name = 'edit_client'),
    ]

view.py

from django.shortcuts import render, redirect
from .models import Client, Location, Lease, Soft_Service, Hard_Service, Safety_Service
from .forms import ClientForm

# Create your views here.
def add_client(request, client_id):
    """Comapany page for updating facilities info"""
    client = Client.objects.get(id = client_id)
    context = 'client':client
    return render(request, 'clients/add_client.html', context)

def all_clients(request):
    '''Shows list of all clients'''
    all_clients = Client.objects.order_by ('date_added')
    context = 'all_clients':all_clients
    return render(request, 'clients/all_clients.html', context)

def index(request):
    """Test Page"""
    return render(request, 'clients/index.html')

def edit_client(request, client_id):
    """Edit an existing Entry."""
    client = Client.objects.get(id=client_id)

    if request.method != 'POST':
        #Inital request; pre-fill form with the current company info.
        form = ClientForm(instance=client)
    else:
        # Post data submitted; process data.
        form = ClientForm(instance=client, data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('clients:all_clients' , client_id=client.id)

    context = 'form': form, 'client': client
    return render(request, 'clients/edit_client.html', context)

edit_client.html

% extends "app/layout.html" %

% block content % % load staticfiles % <p><a href="% url 'clients:add_client' client.id %">Company:  client </a></p>

<h4>See Our Clients</h4>

<<form action="% url 'clients:edit_client<client_id>' client.id %" method="post">
    % csrf_token %
     form.as_p 
    <button name="submit">Save changes</button> </form>
    

% endblock %

all_clients.html

% extends "app/layout.html" %

% block content %
% load staticfiles %
<div class="d-flex" style="height:75px"></div>
    <div class="btn bg-white text-lg-left" style="width:425px">
        <h4>See Our Clients</h4>

        <ul>
            % for add_client in all_clients %
                <li>
                    <a href=" %  url 'clients:add_client' add_client.id %"> add_client </a>
                </li>
            %empty %
                <li> No clients have been added yet. </li>
            % endfor %
        </ul>

        <a class="btn btn-secondary" href=" % url 'clients:new_office' %">Add a new location</a>
<a class="btn btn-secondary" href=" % url 'clients:edit_client' client.id %">Add a new location</a>

    </div>
% endblock content %

【问题讨论】:

请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。 【参考方案1】:

我认为您应该尝试的第一件事是修改 add_clients 页面的 URL,除了您传递的 id 与 all_clients 相同,并且“django 可能会感到困惑”:

#Page for listing all clients
    path('all_clients/', views.all_clients, name = 'all_clients'),

    #Page for adding a new client
    path('add_clients/<int:client_id>/', views.add_client, name = 'add_client'), 

代替:

#Page for listing all clients
    path('all_clients/', views.all_clients, name = 'all_clients'),

    #Page for adding a new client
    path('all_clients/<int:client_id>/', views.add_client, name = 'add_client'),

【讨论】:

感谢您的提示。我进行了更改,虽然对 add_clients.html 的修改工作正常,但 all_clients.html 的问题仍然存在。 签入所有模板,其中有一个指向“all_clients”的链接,后跟一个参数 (client_id)。在你的 URL 中你没有包含它,这就是 Django 找不到 URL 的原因 所以看起来问题与引用 all_clients.html 模板中每个单独客户端的两个超链接有关:&lt;a href=" % url 'clients:add_client' add_client.id %"&gt; add_client &lt;/a&gt;&lt;a class="btn btn-secondary" href=" % url 'clients:edit_client' client.id %"&gt;Add a new location&lt;/a&gt; 当我删除链接时,页面加载正确。您知道在这种情况下创建链接的最佳方法是什么吗?很抱歉格式不好,无法弄清楚如何正确呈现。

以上是关于找不到关键字参数“'client_id': 3”的“all_clients”反向。尝试了 1 种模式:['clients/all_clients/$']的主要内容,如果未能解决你的问题,请参考以下文章

找不到带有参数“(”,)”和关键字参数“”的“view_item”的反向

找不到带有参数“('books',)”和关键字参数“”的“product_list_by_category”的反向

Django新手:“找不到反向”

找替代料总结

设备参数优化更新点完稍后就找不到了

为啥这个 SQL 会告诉我“在预期的地方找不到 FROM 关键字”?