我是 Django 新手
在 urls.py 中,我这样设置路径。我尝试创建一个带有 url 的页面: localhost:8000/topics/1/
urlpatterns = [
path('topics/(?P<topic_id>\d+)/', views.topic, name='topic'),
]
Run Code Online (Sandbox Code Playgroud)
在views.py中。代码是这样的:
def topic(request, topic_id):
"""Show a single topic and all its entries"""
topic = Topic.objects.get(id=topic_id)
entries = topic.entry_set.order_by('date_added')
context = {'topic': topic, "entries": entries}
return render(request, 'learning_logs/topic.html', context)
Run Code Online (Sandbox Code Playgroud)
输入 localhost:8000/topics/1/ 时出现错误 404。
主题/(?P\d+)/ [name='主题']
当前路径 topic/1/ 与其中任何一个都不匹配。
但正确的网址是: localhost:8000/topics/(%3FP1%5Cd+)/
url 中的路径是否有问题或者有什么问题?