Home > Net >  Reverse for 'people' not found. 'people' is not a valid view function or pattern
Reverse for 'people' not found. 'people' is not a valid view function or pattern

Time:10-24

I got the error in my title when I tried to add a URL link so I can get to the person when I click on the URL. Beginner here so sorry if I am making silly mistakes. I am not sure where in my view function I messed up so any help is really appreciated

views.py

from django.shortcuts import  render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import People

# Create your views here.
class pplList(ListView):
    model = People
    context_object_name = 'people'

class pplDetail(DetailView):
    model = People
    context_object_name ='cnd'
    template_name = 'base/people.html'

people_list.html

<h1>Interviewee Dashboard</h1>

<table>
    <tr>
        <th> Item</th>
        <th> </th>

    </tr>
    {% for cnd in people %}
    <tr>
        <td>{{cnd.name}}</td>
        <td><a href="{% url 'people' cnd.id %}">View</a></td>
    </tr>
    {% empty %}
    <h3>No items in list</h3>
    {% endfor %}
</table>

models.py

from unittest.util import _MAX_LENGTH
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
status_choices = (
    ("Choose", "Choose"),
    ("Resume Submitted","Resume Submitted"),
    ("Resume Reviewed", "Resume Reviewed"),
    ("Interview Scheduled","Interview Scheduled" ),
    ("Interview Completed","Interview Completed" ),
   
    
)
wl = (
    ("Choose", "Choose"),
    ("Accepted", "Accepted"),
    ("Rejected", "Rejected"),
)

class People(models.Model):
   # user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    name = models.CharField(max_length=200)
    age = models.IntegerField(null=True, blank=True)
    address = models.TextField(null=True, blank=True)
    position = models.CharField(max_length=200)
    description = models.TextField(null=True, blank=True)
    status = models.CharField(
        max_length = 20,
        choices = status_choices,
        default = '1'
        )
    hired = models.CharField(
        max_length = 20,
        choices = wl,
        default = '1'
        )

    def __str__(self):
        return self.name
    class Meta: 
        ordering = ['hired']

urls.py from base(not root one)

from django.urls import path
from .views import pplList, pplDetail

urlpatterns = [
    path('', pplList.as_view(), name='ppl'),
    path('people/<int:pk>', pplDetail.as_view(), name='ppl'),
]

CodePudding user response:

You cannot give the same name for two urls pattern fisrtly.

from django.urls import path
from .views import pplList, pplDetail

urlpatterns = [
    path('', pplList.as_view(), name='people-list'),
    path('people/<int:pk>', pplDetail.as_view(), name='poeple-detail'),
]

And secondly, you does not use the name of url pattern in your html.

<h1>Interviewee Dashboard</h1>

<table>
    <tr>
        <th> Item</th>
        <th> </th>

    </tr>
    {% for cnd in people %}
    <tr>
        <td>{{cnd.name}}</td>
        <td><a href="{% url 'people-detail' cnd.id %}">View</a></td>
    </tr>
    {% empty %}
    <h3>No items in list</h3>
    {% endfor %}
</table>
  • Related