Please help: Reverse for 'all_clients' with keyword arguments '{'client_id': 3}' not found. 1 pattern(s) tried: ['clients/all_clients/$']
I am new to django and I am having trouble implementing the edit template to my project. I am encountering the following error:
Reverse for 'all_clients' with keyword arguments '{'client_id': 3}' not found. 1 pattern(s) tried: ['clients/all_clients/$']
I have looked on the site for similar occurrences such as Reverse for 'plan_edit' with keyword arguments
but I haven't been able to pin point the issue. I believe the issue arises when I add a hyperlink to my all_clients.html template. Also, the template pages for /clients/edit_client/?/ will load, however after submission using the save changes button the NoReserse Match error resurfaces as it attempts to load the clients/all_clients page.
Any help on this matter would be greatly appreciated. See code below:
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
class Location(models.Model):
#Location holds headquarters and satellite facilities info
client = models.ForeignKey(Client, on_delete = models.CASCADE)
text = models.CharField('Address', default = 'Address', max_length = 250)
address2 = models.CharField('Address 2', max_length = 250)
city = models.CharField(max_length = 250)
state = models.CharField(max_length = 250)
zipcode=models.IntegerField(default = 00000)
def __str__(self):
"""Return a string representation of the model."""
return self.text
class Lease(models.Model):
#Leasing information for company's facilities
client = models.ForeignKey(Client, on_delete = models.CASCADE)
text = models.CharField('Leasee', default = 'Leasee', max_length = 150)
rate = models.FloatField(default = 0.00)
location = models.ForeignKey(Location, on_delete = models.CASCADE)
date = models.DateField('Start Date')
def __str__(self):
"""Return a string representation of the model."""
return self.text
class Soft_Service(models.Model):
#soft facility serives include items not related to physical facility
client = models.ForeignKey(Client, on_delete = models.CASCADE)
text = models.CharField('Soft_Service', default = 'Soft_Service', max_length = 250)
security_crew_title = models.CharField(max_length = 200)
cleaning_crew_title = models.CharField(max_length = 200)
landscaping_crew_title = models.CharField(max_length = 200)
caterer = models.CharField(max_length = 200)
def __str__(self):
"""Return a string represntation of the model."""
return self.text
class Safety_Service(models.Model):
#Safety services include items such as first aid training
client = models.ForeignKey(Client, on_delete = models.CASCADE)
text = models.CharField('Safety_Service', default = 'Safety_Service', max_length = 250)
training_schedule = models.DurationField('training schedule')
audit_schedule = models.DurationField('audit schedule')
saftety_meetings = models.DateTimeField ('meeting schedule')
safety_coordinator = models.CharField(max_length = 150)
job_safety_analysis = models.CharField (max_length = 500)
def __str__(self):
"""Return a string representation of the model."""
return self.text
class Hard_Service(models.Model):
#Hard services include overhead services such as utilities
client = models.ForeignKey(Client, on_delete = models.CASCADE)
text = models.CharField('Hard_Service', default = 'Hard_Service', max_length = 250)
electric_provider = models.CharField(max_length = 200)
plumbing_provider = models.CharField(max_length = 200)
hvac_provider = models.CharField(max_length = 200)
mechanical_provider = models.CharField(max_length = 200)
fire_safety_provider = models.CharField(max_length = 200)
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 new_office(request):
'''Add a new office'''
if request.method != 'POST':
#No data submitted; create a blank form
form = ClientForm()
else:
# POST data submitted; process data
form = ClientForm(data = request.POST)
if form.is_valid():
form.save()
return redirect('clients:all_clients')
#Display a blank or invalid form.
context = {"form": form}
return render(request, 'clients/new_office.html', context)
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)
layout.html
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{ title }} - My Django Application</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="{% static 'app/content/site.css' %}" />
<script src="{% static 'app/scripts/modernizr-2.6.2.js' %}"></script>
</head>
<body>
<div class="navbar navbar-inverse navbar-expand-sm bg-dark navbar-dark fixed-top">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a href="/" class="navbar-brand"><span class="font-weight-bold">Built</span> / <span class="font-weight-lighter">Command</span></a>
</div>
<div class="d-flex justify-content-end">
<div class="container">
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="nav navbar-nav">
<li><a class="nav-link mr-auto p-2" href="{% url 'home' %}">Home</a></li>
<li><a class="nav-link" href="{% url 'about' %}">About</a></li>
<li><a class="nav-link" href="{% url 'contact' %}">Contact</a></li>
<!--<li><a class="nav-link" href="{% url 'clients:index' %}">HypeSpace Test Page</a></li>-->
<li><a class="nav-link" href="{% url 'clients:all_clients' %}">Clients</a></li>
</ul>
{% include 'app/loginpartial.html' %}
</div>
</div>
</div>
</div>
</div>
<div class="container body-content">
{% block content %}{% endblock %}
<hr/>
<footer>
<p>© {{ year }} - Built / Command</p>
</footer>
</div>
<script src="{% static 'app/scripts/jquery-1.10.2.intellisense.js' %}"></script>
<script src="{% static 'app/scripts/jquery-1.10.2.js' %}"></script>
<script src="{% static 'app/scripts/bootstrap.js' %}"></script>
<script src="{% static 'app/scripts/respond.js' %}"></script>
{% block scripts %}{% endblock %}
</body>
</html>
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 %}
First thing i think you should try is modifying the URL to the add_clients page, aside from the id you are passing is identical to all_clients, and "django may get confused":
#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'),
instead of:
#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'),
First thing i think you should try is modifying the URL to the add_clients page, aside from the id you are passing is identical to all_clients, and "django may get confused":
#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'),
instead of:
#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'),