[英]I am having troubles with a 404-error message from Django. It says “No location found matching the query”
So I am taking the One Month Python course online and I am having some issues. 因此,我正在在线上学习一个月Python课程,但遇到了一些问题。 The thing is that my general expression
(?P<pk>\\d+)/detail/$
does not seem to work hence the 404-message. 问题是我的一般表达式
(?P<pk>\\d+)/detail/$
似乎不起作用,因此出现了404消息。
Here is my code: main urls.py 这是我的代码:main urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns ('',
# blah blah
# blah blah blah
# Blaaaahhh
url(r'^admin/', include(admin.site.urls)),
(r'', include('core.urls')),
)
core/urls.py 核心/ urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
import core.views as coreviews
urlpatterns = patterns ('',
url(r'^$', coreviews.LandingView.as_view()),
url(r'location/$', coreviews.LocationListView.as_view()),
url(r'location/(?P<pk>\d+)/detail/$', coreviews.LocationDetailView.as_view()),
)
Views: 浏览次数:
from django.shortcuts import render
from django.views.generic.base import TemplateView
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
import core.models as coremodels
# Create your views here.
class LandingView(TemplateView):
template_name = "base/index.html"
class LocationListView(ListView):
model = coremodels.Location
template_name = 'location/list.html'
class LocationDetailView(DetailView):
model = coremodels.Location
template_name = 'location/detail.html'
context_object_name = 'location'
I have already created five locations using the Django admin site: https://gyazo.com/1db0b35fafdacb3d4991fbea3990ee61 我已经使用Django管理网站创建了五个位置: https : //gyazo.com/1db0b35fafdacb3d4991fbea3990ee61
Here is the actual error message: 这是实际的错误消息:
The error message "No location found matching the query"
suggests that the url pattern is correct, but there is no location with pk=1
in the database. 错误消息
"No location found matching the query"
表明网址格式正确,但是数据库中没有pk=1
位置。 You probably deleted it. 您可能删除了它。
Try a different url eg /location/2/detail/
for a primary key that definitely exists. 尝试使用其他网址,例如
/location/2/detail/
,以获取肯定存在的主键。
You can find a primary key by doing something like Location.objects.first().id
in the shell, or simply clicking on a location in the Django admin, and looking at the url. 您可以通过在外壳中执行类似于
Location.objects.first().id
来找到主键,或者只需单击Django admin中的某个位置,然后查看url。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.