【发布时间】:2021-09-23 03:50:33
【问题描述】:
我有两个 urls.py 文件
第一个用于项目 urls.py
urlpatterns = [
path('admin/', admin.site.urls , name = 'admin'),
path('',homeView, name='home'),
path('post/',post, name='post'),
path('post/',include('post.urls'), name='posturl'),
path('student/',studentM, name = 'student' ),
path('student/',include('student.urls') , name='studenturl'),
]
第二个是应用的网址
urlpatterns = [
path('index', post_index, name = 'index'),
path('details/' + '<int:id>', post_detail, name = 'detail'),
path('create', post_create, name = 'create'),
path('update', post_update , name = 'update'),
path('delete', post_delete , name = 'delete'),
]
我正在尝试像这样到达地址 :8000/post/details/5 但是当我尝试在 html 中使用时
<a href = "{ % url 'post.detail' %}"> =>> {{post.id}}</a>
<a href = "{ % url 'detail' %}"> =>> {{post.id}}</a>
<a href = "{ % url 'post' %}"> =>> {{post.id}}</a>
这些都不起作用我收到 404 错误。
我怎样才能到达我想要的链接
def post(request):
context = {
}
return render(request,'post/post.html',context)
def post_detail(request,id):
post = get_object_or_404(Post,id=id)
context = {
'post' : post
}
return render(request,"post/details.html",context)
【问题讨论】:
-
发布详细信息需要一个您没有从模板传递的 id。此外,根据命名空间,url 的设计也不是很好,请通过官方文档查看相同的docs.djangoproject.com/en/3.2/topics/http/urls。同时你可以检查这一行并尝试找出其他人: =>> {{post.id}} 使用: {{post.id}}
标签: django django-models django-views django-forms django-templates