regular expression error in DJango trying to find

2019-03-07 07:27发布

问题:

I have the following error while trying to load up specific index numbers (relating to blog posts) using Django.

The erroneous code is below - can anyone help point out the error?

        path(r'(?P)<pk>\d+)', DetailView.as_view(model = Post,template_name = 'blog/post.html'))

The whole code on that urls.py file to show context is here:

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'))

] The URL I am trying to reach is:

http://127.0.0.1:8000/blog/2

and the error on page is:

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.

Succinctly, I need help in finding the error in the following code snippet (the first path works fine, it is the SECOND PATH that doesn't)

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 changed the code to remove the erroneous bracket to this:

path(r'(?P<pk>\d+)', DetailView.as_view(model = Post,template_name = 'blog/post.html'))]

But it still doesn't work...

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.

Trying suggested answer I tried using this, but the error persists

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'))]

and tried this too:

path(r'(?P<int:pk>\d+', DetailView.as_view(model = Post,template_name = 'blog/post.html'))]

along with this:

path(r'(?P<int:pk>\d+)', DetailView.as_view(model = Post,template_name = 'blog/post.html'))]

Errors persist

  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.

回答1:

The problem is that you are confused between the old regex-based URL mechanism and the new path-based one. If you use path, you should use the new special syntax:

path('<int:pk>', DetailView.as_view(...))

whereas if you want to use regexes, you should use the old url function (or the new alias, re_path):

url(r'(?P<pk>\d+)', DetailView.as_view(...))