Mis*_*ing 0 django url expression
尝试使用 Django 加载特定索引号(与博客文章相关)时出现以下错误。
错误代码如下- 任何人都可以帮助指出错误吗?
path(r'(?P)<pk>\d+)', DetailView.as_view(model = Post,template_name = 'blog/post.html'))
Run Code Online (Sandbox Code Playgroud)
该 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'))
Run Code Online (Sandbox Code Playgroud)
] 我试图访问的 URL 是:
http://127.0.0.1:8000/blog/2
Run Code Online (Sandbox Code Playgroud)
页面上的错误是:
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.
Run Code Online (Sandbox Code Playgroud)
简而言之,我需要帮助来查找以下代码片段中的错误(第一个路径工作正常,第二个路径没有)
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'))
]
Run Code Online (Sandbox Code Playgroud)
更新 我更改了代码以删除错误的括号:
path(r'(?P<pk>\d+)', DetailView.as_view(model = Post,template_name = 'blog/post.html'))]
Run Code Online (Sandbox Code Playgroud)
但它仍然不起作用......
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.
Run Code Online (Sandbox Code Playgroud)
尝试建议的答案 我尝试使用它,但错误仍然存在
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'))]
Run Code Online (Sandbox Code Playgroud)
也试过这个:
path(r'(?P<int:pk>\d+', DetailView.as_view(model = Post,template_name = 'blog/post.html'))]
Run Code Online (Sandbox Code Playgroud)
伴随着这个:
path(r'(?P<int:pk>\d+)', DetailView.as_view(model = Post,template_name = 'blog/post.html'))]
Run Code Online (Sandbox Code Playgroud)
错误仍然存在
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.
Run Code Online (Sandbox Code Playgroud)
问题是您混淆了旧的基于正则表达式的 URL 机制和新的基于路径的机制。如果使用path,则应使用新的特殊语法:
path('<int:pk>', DetailView.as_view(...))
Run Code Online (Sandbox Code Playgroud)
而如果你想使用正则表达式,你应该使用旧url函数(或新别名,re_path):
url(r'(?P<pk>\d+)', DetailView.as_view(...))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
643 次 |
| 最近记录: |