我应该如何在django中制作详细信息,给予404

发布于 2025-01-31 01:43:41 字数 539 浏览 1 评论 0原文

在文件“ 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下一个:

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

并创建了模板“ lotess_view.html”。 当我写信给浏览器时:

404详细信息中的错误

in file "views.py" i added next code:

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

then in the file urls.py I added to urlpatterns next:

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

and created the template "details_view.html".
When i write to browser:
http://127.0.0.1:8000/news/1

it gives 404 error
404 error in DetailView

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仄言 2025-02-07 01:43:41

转到< admin站点,并添加至少一个articles对象。

另外,您可以在Shell中创建它:

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

Go to Admin Site and add at least one Articles object.

Alternatively, you can create it in shell:

$ python manage.py shell
>>> from yourapp.models import Articles
>>> Articles.objects.create(...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文