反转为'all_clients',找不到关键字参数"{'client_id":3}。尝试了 1 种模式: ['客户端/all_clients/$']



我是新的Django和我有麻烦实现编辑模板到我的项目。我遇到以下错误:

没有找到带有关键字参数'{'client_id': 3}'的'all_clients'。['clients/all_clients/$']

我在网站上查看了类似的事件,例如使用关键字参数

反转'plan_edit'但我还没能指出这个问题。我认为,当我添加一个超链接到我的all_clients.html时,问题就出现了模板。此外,/clients/edit_client/?/将加载,但是在使用save changes按钮提交后,当它试图加载clients/all_clients时,NoReserse Match错误重新出现。页面。

见下面代码:

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 %}

我认为你应该尝试的第一件事是修改URL到add_clients页面,除了你传递的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'),

相关内容

  • 没有找到相关文章

最新更新