Django.urls.exceptions.NoReverseMatch


#1

I have been busy coding away trying to set up my
site but have run into a dilemma and at the same time, bit of a
problem. I was wondering if you would be kind enough to offer some
advice on how I should approach building my site.

Where do I start, well, I have various clients, who each have
various contacts, equipment, which at the moment I am calling
servers and also a list of FAQ’s. I just want my index page as a page of
its own with perhaps news and also introduction. I then want to be
able to select a client and from then on, will be able to select
only that particular clients servers, contacts and FAQ’s etc. As
you can see from my models, it also involves foreign keys etc.

I am not quite sure how to implement this although I think I am on
the right track. However, I am being plagued by an error at the
moment which I have been trying to solve for the last couple of
days. No matter how I google this, the solutions don’t seem to
work. The error is as follows:-

django.urls.exceptions.NoReverseMatch: Reverse for ‘client_detail’
with arguments ‘()’ and keyword arguments ‘{u’slug’: ‘’}’ not
found. 1 pattern(s) tried:
[‘client_detail/(?P[-\w]+)/$’]

I am running Django 1.10, which I upgraded to, thinking that it
was something I had done that was incompatible with 1.9 which I
had originally installed.

I’d be really grateful if you could assist me with this error and
also offer some guidance if I am going about this the right way.
If you’d rather me put this up on the forum, then I will do so.

Regards

David

  clients.html

  {% extends 'base.html' %}

  {% block title %}

      Clients - {{ block.super }}

  {% endblock title %}

  

  {% block content %}

      {% for client in clients %}

          <h2><a href="{% url 'client_detail'
  slug=client.slug %}">{{ client.name }}</a></h2>

          <p>{{ client.town }}</p>

      {% endfor %}

  {% endblock content %}

    

  

  client_detail.html

    {% extends 'base.html' %}

  {% block title %}

      {{ client.name }} - {{ block.super }}

  {% endblock title %}

  

  {% block content %}

          <h1>{{ client.name }} </h1>

          <p>{{ client.town }}</p>

  {% endblock content %}

  

  

  

  models.py

  from __future__ import unicode_literals

  

  from django.contrib.auth.models import Permission, User

  from django.db import models

  

  

  class Client(models.Model):

      user = models.ForeignKey(User, default=1)

      is_enabled = models.BooleanField(default = True)

      name = models.CharField(max_length = 50)

      address1 = models.CharField(max_length = 50)

      address2 = models.CharField(max_length = 50, null = True,
  blank = True)

      address3 = models.CharField(max_length = 50, null = True,
  blank = True)

      town = models.CharField(max_length = 30)

      county = models.CharField(max_length = 30, null = True, blank
  = True)

      postcode = models.CharField(max_length = 10)

      faq_url = models.URLField(null = True, blank = True)

      sla = models.CharField(max_length = 100, null = True, blank =
  True)

      site_managers = models.CharField(max_length = 100, null =
  True, blank = True)

      site_champion = models.CharField(max_length = 50, null = True,
  blank = True)

      notes = models.TextField(null = True, blank = True)

      logo = models.ImageField(null = True, blank = True)

      is_favourite = models.BooleanField(default = False, blank =
  True)

      

      def __str__ (self):

          return self.name + ', ' + self.town

  

  

  

  class Contact(models.Model):

      client = models.ForeignKey(Client, on_delete = models.CASCADE)

      is_enabled = models.BooleanField(default = True)

      firstname = models.CharField(max_length = 30)

      secondname = models.CharField(max_length = 30)

      position = models.CharField(max_length = 30, null = True,
  blank = True)

      telephone = models.CharField(max_length = 30, null = True,
  blank = True)

      extension = models.CharField(max_length = 10, null = True,
  blank = True)

      mobile = models.CharField(max_length = 30, null = True, blank
  = True)

      email = models.EmailField(null = True, blank = True)

      notes = models.TextField(null = True, blank = True)

      is_favourite = models.BooleanField(default = False, blank =
  True)

      

      def __str__ (self):

          return self.firstname + ' ' + self.secondname

      

      

  

  class Server(models.Model):

      client = models.ForeignKey(Client, on_delete = models.CASCADE)

      is_enabled = models.BooleanField(default = True)

      test = models.BooleanField(default = False, blank = True)

      name = models.CharField(max_length = 50, blank = True)

      detail = models.CharField(max_length = 200, null = True, blank
  = True)

      notes = models.TextField(null = True, blank = True)

      OS = models.CharField(max_length = 30, null = True, blank =
  True)

      OS_version = models.CharField(max_length = 30, null = True,
  blank = True)

      ssh_ip1 = models.GenericIPAddressField(null = True, blank =
  True)

      ssh_ip2 = models.GenericIPAddressField(null = True, blank =
  True)

      ssh_ip3 = models.GenericIPAddressField(null = True, blank =
  True)

      ssh_username = models.CharField(max_length = 30, null = True,
  blank = True)

      ssh_pword = models.CharField(max_length = 30, null = True,
  blank = True)

      ilo_ip = models.GenericIPAddressField(null = True, blank =
  True)

      ilo_username = models.CharField(max_length = 30, null = True,
  blank = True)

      ilo_pword = models.CharField(max_length = 30, null = True,
  blank = True)

      gui_url = models.URLField(null = True, blank = True)

      gui_username = models.CharField(max_length = 30, null = True,
  blank = True)

      gui_pword = models.CharField(max_length = 30, null = True,
  blank = True)

      root_pword = models.CharField(max_length = 30, null = True,
  blank = True)

      is_favourite = models.BooleanField(default = False)

  

      def __str__ (self):

          return self.name

  

  

  

  class Faq(models.Model):

      client = models.ForeignKey(Client, on_delete = models.CASCADE)

      is_enabled = models.BooleanField(default = True)

      test = models.BooleanField(default = False, blank = True)

      notes = models.TextField(null = True, blank = True)

      

      def __str__ (self):

          return self.client

  

  

  

  views.py

  from django.shortcuts import render

  from clientapp.models import Client, Contact, Server, Faq

  

  def index(request):

      clients = Client.objects.all().order_by('name')

      return render(request, 'index.html', {'clients': clients, })

  

  

  def clients(request):

      clients = Client.objects.all()

      return render(request, 'clients/clients.html', {'clients':
  clients, })

  

  

  def contacts(request):

      ccontacts = Contact.objects.all()

      return render(request, 'contacts/contacts.html', {'contacts':
  contacts, })

  

  

  def servers(request):

      servers = Server.objects.all()

      return render(request, 'servers/servers.html', {'servers':
  servers, })

  

  

  def faqs(request):

      faqs = Faq.objects.all()

      return render(request, 'faqs/faqs.html', {'faqs': faqs, })

  

  

  def client_detail(request, slug):

      ## grab the object...

      client = Client.objects.get(slug=slug)

      # and pass to the template

      return render(request, 'clients/client_detail.html',
  {'client': client, })

  

  

  def contact_detail(request, slug):

      ## grab the object...

      contact = Client.objects.get(slug=slug)

      # and pass to the template

      return render(request, 'contacts/contact_detail.html',
  {'contact': contact, })

  

  

  def server_detail(request, slug):

      ## grab the object...

      server = Server.objects.get(slug=slug)

      # and pass to the template

      return render(request, 'servers/server_detail.html',
  {'server': server, })

  

  

  def faq_detail(request, slug):

      ## grab the object...

      faq = Faq.objects.get(slug=slug)

      # and pass to the template

      return render(request, 'faqs/faq_detail.html', {'faq': faq, })

  

  

  

  urls.py

  from django.conf.urls import url

  from django.contrib import admin

  from django.views.generic import TemplateView

  from clientapp import views

  

  

  urlpatterns = [

      url(r'^$', views.index, name='home'),

      url(r'^about/$',
  TemplateView.as_view(template_name='about.html'), name='about'),

      url(r'^contactme/$',
  TemplateView.as_view(template_name='contactme.html'),
  name='contactme'),

      url(r'^clients/$', views.clients, name='clients'),    

      url(r'^contacts/$', views.contacts, name='contacts'),    

      url(r'^servers/$', views.servers, name='servers'),    

      url(r'^faqs/$', views.faqs, name='faqs'),    

      url(r'^client_detail/(?P<slug>[-\w]+)/$',
  views.client_detail, name='client_detail'),    

      url(r'^contact_detail/(?P<slug>[-\w]+)/$',
  views.contact_detail, name='contact_detail'),    

      url(r'^server_detail/(?P<slug>[-\w]+)/$',
  views.server_detail, name='server_detail'),    

      url(r'^faq_detail/(?P<slug>[-\w]+)/$', views.faq_detail,
  name='faq_detail'),    

      url(r'^admin/', admin.site.urls),

  ]

#2

This error means that it cannot find that specific page/object. In the HTML, do you have a link that is just /client_detail/ (without any following slug)?

I get this kind of error myself when my app is generating links and it generate a links that would 404, so it just breaks instead.


#3

Hi Tracy,

I managed to resolve this myself but it was a few days ago when I was having this problem and I have forgotten exactly what I did to correct it. However, it was something along the lines of what you have said coupled with a typo.


#4

Okay, thanks for following up! :)