我正在做这个简单的公司评估应用程序,可能有详细的视图。而列表视图正常显示,每当我点击查看它的详细信息时,我都会看到页面未找到(404)错误。它的工作原理是,如果你要点击一个部门(列表视图),它必须带你到一个显示徽标的详细信息页面,以及该部门下的帖子。
回溯;
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/1//
Using the URLconf defined in Employee.urls, Django tried these URL patterns, in this order:
^admin/
^appraisal/
^ ^$ [name='index']
^ ^(?P<pk>[0-9]+)/$ [name='detail']
^static\/(?P<path>.*)$
^media\/(?P<path>.*)$
The current URL, 1//, didn't match any of these.
我的观点;
from django.views import generic
from .models import Department
class IndexView(generic.ListView):
template_name = "index.html"
context_object_name = "departments"
def get_queryset(self):
return Department.objects.all()
class DetailView(generic.DetailView):
model = Department
template_name = "detail.html"
我的网址;
项目/网址
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^appraisal/', include('Appraisal.urls')),
url(r'^', include('Appraisal.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
应用程序/网址;
from django.conf.urls import url
from . import views
app_name = 'Appraisal'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
]
我的模特;
class Department(models.Model):
Name = models.CharField(max_length=30)
Leader = models.ForeignKey(Employee)
Department_logo = models.FileField()
is_favorite = models.BooleanField(default=False)
def __str__(self):
return '{}'.format(self.Name)
class Post(models.Model):
Title = models.CharField(max_length=30)
Department = models.ForeignKey(Department, on_delete=models.CASCADE)
is_favorite = models.BooleanField(default=False)
def __str__(self):
return '{}'.format(self.Title)
有人帮助我解决这个问题,我知道这真的是一件让它无法解决的小事。
谢谢!
答案 0 :(得分:0)
您的链接错误http://127.0.0.1:8000/1//
最后一个斜线太多了
要正确匹配的网址<{1}}或http://127.0.0.1:8000/1/
你可能在模板中建立了错误的链接。