python - 找不到页面 (404) 请求方法:GET 请求 URL:http://127.0.0.1:4000/person/persondetails/3
问题描述
人员列表在 中完美显示person.html
,但是当我单击 persondetails
锚标记时出现错误。请让我知道如何纠正这个问题。
项目
urls.py
from django.conf.urls import url, include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^',include('example.urls')), ]
应用程序
urls.py
from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.HomePageView.as_view(), name='home'), url(r'^about/$', views.AboutPageView.as_view(), name='about'), url(r'^data/$', views.DataPageView.as_view(), name='data'), url(r'^person/$', views.PersonPageView.as_view(), name='person'), url(r'^persondetails/(?P<pk>[-\w]+)/$', views.PersonDetailPageView.as_view(), name='persondetails'), ]
views.py
class PersonDetailPageView(DetailView): def details(self,pk): personDetail=get_object_or_404(Person, pk=self.kwargs.get("pk")) return redirect(request,'example/persondetails.html', {'personDetail':personDetail})
person.html
<tbody> {% for persons in person %} <tr> <td><a href="persondetails/{{persons.pk}}"> {{persons.first_name}}</a></td> <td><a href="persondetails/{{persons.pk}}"> {{persons.first_name}}</a></td> <td>{{persons.last_name}}</td> <td>{{persons.email}}</td> </tr> {% endfor %} </tbody>
解决方案
在您的 urls.py 中,您已添加,persondetails/pk..
因此它会将您重定向到 url,例如 id 3,
http://127.0.0.1:4000/persondetails/3/
您可以将其添加到 ..person/persondetail/pk...
为了更好的安排,views.py,
class PersonDetailPageView(DetailView):
model = Your_Model_Name
template_name = 'your_template_name.html'
在 html 中,
<a href="{% url 'persondetails' person.id %}"> your template </a>
推荐阅读
- python - 使用 Selenium(Python) 将 Youtube 质量降低到 144p
- javascript - 如何使用 Fuse.js 过滤 vuetify v-autocomplete 组件中的数据?
- php - 使用服务帐户和 PHP 上传 Google 驱动器文件
- jquery-ui - 如何将对象拖放到 three.js Mesh 中?
- postgresql - 是否可以为 Select 语句创建存储过程?
- javascript - 刷新页面后如何在同一页面保存选择的 Jquery 选择的选项,甚至保持在同一位置a
- scala - 了解具有两个成员的案例类的 Scala 匹配
- linux - 如何通过别名更新 DTS 节点的属性?
- c# - 将远程桌面上的 WPF 动画平滑到 VM
- java - 模型映射器在测试类上调用方法时获取空指针异常