关于用网页正确制作 link 一个 URL 的问题
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>
我是一名初学者,正在从事一个项目,该项目旨在制作一个基于 Web 的应用程序,该应用程序显示特定位置的 covid 病例数据。我现在正在尝试制作一个网页,显示有关某个位置的一些简单信息。
此网页的 URL 应该是 http://localhost:8000/database/location_data/1
。但是,当我测试该网页时,它无法显示并显示错误消息:
Generic detail view viewLocationData must be called with either an object pk or a slug in the URLconf.
我不确定到底是哪一部分出了问题以及如何解决它。
您需要传递对象标识符 pk 或 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.
试试这个
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')
]
已更新:
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
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>
我是一名初学者,正在从事一个项目,该项目旨在制作一个基于 Web 的应用程序,该应用程序显示特定位置的 covid 病例数据。我现在正在尝试制作一个网页,显示有关某个位置的一些简单信息。
此网页的 URL 应该是 http://localhost:8000/database/location_data/1
。但是,当我测试该网页时,它无法显示并显示错误消息:
Generic detail view viewLocationData must be called with either an object pk or a slug in the URLconf.
我不确定到底是哪一部分出了问题以及如何解决它。
您需要传递对象标识符 pk 或 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.
试试这个
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')
]
已更新:
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