Djangoを使用して特定のインデックス番号(ブログ記事に関する)を読み込もうとしましたが、次のエラーが発生します。DJangoの正規表現エラーがblog.htmlでインデックス番号を検索しようとしています
エラーコードは、以下です。誰でもエラーを指摘できますか?
path(r'(?P)<pk>\d+)', DetailView.as_view(model = Post,template_name = 'blog/post.html'))
コンテキストを表示することurls.pyファイルの全コードはここにある:
from django.urls import path
from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from blog.models import Post
#it's already going to blog/, so this regular expression is just blank
urlpatterns = [
path(r'', ListView.as_view(queryset=Post.objects.all().order_by("-date") [:25],
template_name="blog/blog.html")),
path(r'(?P)<pk>\d+)', DetailView.as_view(model = Post,template_name = 'blog/post.html'))
] は、私が到達しようとしているURLは、:
http://127.0.0.1:8000/blog/2
であり、ページ上のエラーは:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/2
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
blog/
blog/ (?P)<pk>\d+)
The current path, blog/2, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
簡潔に、私は次のコードスニペットでエラーを見つけるのに役立つ必要
urlpatterns = [
path(r'', ListView.as_view(queryset=Post.objects.all().order_by("-date") [:25],
template_name="blog/blog.html")),
path(r'(?P)<pk>\d+)', DetailView.as_view(model = Post,template_name = 'blog/post.html'))
]
UPDATE I(最初のパスが正常に動作しますが、それがない第2の経路があります)
...path(r'(?P<pk>\d+)', DetailView.as_view(model = Post,template_name = 'blog/post.html'))]
しかし、それはまだ動作しません。これに誤ったブラケットを削除するには、コードを変更私はこれを使用してみましたが、エラーが
urlpatterns = [
path(r'', ListView.as_view(queryset=Post.objects.all().order_by("-date") [:25],
template_name="blog/blog.html")),
path(r'<int:pk>\d+', DetailView.as_view(model = Post,template_name = 'blog/post.html'))]
を持続し、あまりにもこれを試してみまし提案答え をしようと
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
blog/
blog/ (?P<pk>\d+)
The current path, blog/2, didn't match any of these.
:このとともに
path(r'(?P<int:pk>\d+', DetailView.as_view(model = Post,template_name = 'blog/post.html'))]
を:
path(r'(?P<int:pk>\d+)', DetailView.as_view(model = Post,template_name = 'blog/post.html'))]
エラーは問題が古い正規表現ベースのURLメカニズムと新しいパスベースの一方との間に混乱しているということである
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
blog/
blog/ (?P<int:pk>\d+)
The current path, blog/2, didn't match any of these.
をこれについてDjangoや他の場所のドキュメントはありますか?この提案された新しい構文で、行全体を正しく書き直すことができるので、私はそれをテストできますか?多くのありがとう – MissComputing
私はあなたの提案を試みましたが、エラーが続く>アップデートを参照してください: – MissComputing
しかし、それは全く何も言わなかったのです。 –