python - Django 2.1.2:通用详细视图 SchoolDetailView 必须使用 URLconf 中的对象 pk 或 slug 调用
问题描述
我是 django 的新手,不明白什么是 pk 或 slug。到底是怎么回事?
模型.py:
class School(models.Model):
name = models.CharField(max_length=256)
principal = models.CharField(max_length=256)
location = models.CharField(max_length=256)
def __str__(self):
return self.name
模板页面:
<a class="navbar-brand" href="{% url 'basic_app:list'%}">Schools</a>
网址.py:
path('',views.SchoolDetailView.as_view(),name='list'),
视图.py:
class SchoolDetailView(DetailView):
context_object_name = 'school_detail'
model = models.School
template_name = 'basic_app/school_detail.html'
解决方案
详细视图用于获取特定对象的详细信息。为了做到这一点,您必须在您的网址中传递 pk 。
urlpatterns = [
path('<int:pk>/', SchoolDetailView.as_view(), name='school-detail'),
]
如果要显示对象列表,请使用 ListView
推荐阅读
- php - PHP Session 不能立即工作,但在刷新页面后工作
- python - 将什么作为 post_save 接收者的“发送者”?
- c# - 为 Android 编译 Unity 游戏时,它仍在使用 Android.arch Repo 所以我如何告诉 Unity 使用 AndroidX
- java - 如何测量Android循环中代码的累积执行时间?
- php - 不能使用“对象”作为类名,因为它是保留的
- sql - 如何根据键模式从 Postgress 中的 json 中提取值?
- c++ - 当用户在 C++ 的同一行中同时输入字符串和整数时,如何拆分字符串和整数?
- javascript - 使用 Javascript 比较用户输入
- java - 在 ListView 中显示文件名并存储文件路径
- html - 如何对齐图标字体和按钮内的文本?