Problem about making correctly link a URL with a webpage

models.py

from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
#from django.contrib.postgres.fields import ArrayField

# Create your models here.
class locationData (models.Model):
    locationID = models.AutoField (primary_key = True)
    name = models.CharField (max_length = 64)
    population = models.IntegerField (default = 0, validators = [MinValueValidator(0)])
    apiEndpoint = models.URLField (max_length = 256)
    resourceURL = models.URLField (max_length = 256) 
    
    
class dateData (models.Model):
    entryID = models.AutoField (primary_key = True)
    name = models.CharField (max_length = 64)
    date = models.DateField (auto_now = False, auto_now_add = False)
    confirmedCase = models.IntegerField (default = 0, validators = [MinValueValidator(0)])
    deathCase = models.IntegerField (default = 0, validators = [MinValueValidator(0)])

views.py

from django.shortcuts import render
from django.views.generic import TemplateView, ListView, DetailView
from database.models import locationData, dateData

# Create your views here.
class viewLocationData (DetailView):
    template_name = "locationData.html"
    model = locationData
    
    def get_context_data (self,**kwargs):
        location = self.kwargs['location']
        
        context = super().get_context_data (**kwargs)
        context ['location'] = locationData.objects.get (pk = location)
        return context

app/urls.py

from django.urls import path
from database import views

urlpatterns = [
    path ('location_data/<int:location>',
    views.viewLocationData.as_view(),
    name = 'location-data')
]

config/urls.py

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path ('database/', include ('database.urls'))
]

locationData.html

<h1>Location Data</h1>

<table>
    <tr>
        <td>Location Name</td>
        <td>{{location.name}}</td>
    </tr>
    <tr>
        <td>Current Estimated Population</td>
        <td>{{location.population}}</td>
    </tr>
    <tr>
        <td>API Endpoint</td>
        <td>{{location.apiEndpoint}}</td>
    </tr>
    <tr>
        <td>URL of Resource</td>
        <td>{{location.resourceURL}}</td>
    </tr>
</table>

I am a beginner working on a project to make a web-based application that shows covid case data of a specific location. I am now trying to make a webpage that displays some simple info about a location.

This webpage’s URL is supposed to be http://localhost:8000/database/location_data/1. However, when I am testing that webpage, it cannot be displayed and shown an error message:

Generic detail view viewLocationData must be called with either an object pk or a slug in the URLconf.

I am not sure exactly which part is going wrong and how to fix it.

Answer

You need to pass an object identifier pk or slug.

The URLconf here uses the named group pk – this name is the default name that DetailView uses to find the value of the primary key used to filter the queryset.

try this

urlpatterns = [
    path ('location_data/<int:pk>', views.viewLocationData.as_view(), name = 'location-data')

    or 

    path ('location_data/<slug:slug>', views.viewLocationData.as_view(), name = 'location-data')

]

Updated:

class viewLocationData (DetailView):
    template_name = "locationData.html"
    model = locationData
    
    def get_context_data (self,**kwargs):
        location = self.kwargs['pk']
        
        context = super().get_context_data (**kwargs)
        context ['location'] = locationData.objects.get (pk = location)
        return context

https://docs.djangoproject.com/en/3.1/ref/class-based-views/generic-display/#detailview

Leave a Reply

Your email address will not be published. Required fields are marked *