我应该如何使DetailView在Django中,给出404



in file "views.py"我添加了下一个代码:

from django.views.generic import DetailView
class NewsDetailView(DetailView):
model = Articles
template_name = "news/details_view.html"
context_object_name = 'article'

然后在文件urls.py中我添加了urlpatterns next:

path('<int:pk>', views.NewsDetailView.as_view(), name="news_detail"),

并创建了模板"details_view.html"当我写信给浏览器:http://127.0.0.1:8000/新闻/1

给出404错误在DetailView中出现404错误

进入Admin Site,至少添加一个Articles对象

或者,您可以在shell中创建它:

$ python manage.py shell
>>> from yourapp.models import Articles
>>> Articles.objects.create(...)

最新更新