404错误,获取对象或404 django

时间:2018-04-08 09:46:45

标签: python django python-3.x django-models django-views

嘿伙计们我已经建立了一个博客网站,如果我输入这样的数字:blog / 1,我应该得到第一个博客或者我应该得到404错误!但我只得到404错误,而我有博客!我的项目名称是portfolio-project,urls.py看起来像:

from django.contrib import admin

from django.urls import path, include

from django.conf import settings

from django.conf.urls.static import static

import blog.views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', blog.views.home, name='home'),
    path('blog/', include('blog.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

然后转到blog.urls,它看起来像:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.home, name='allblogs'),
    path('<int:blog_id>/', views.detail, name='detail'),
]

因为我把博客/ 1转到了views.detail文件,如下所示:

from django.shortcuts import render, get_object_or_404
from .models import Blog

def home(request):
    blogs = Blog.objects
    return render(request, 'blog/home.html', {'blogs':blogs})

def detail(request, blog_id):
    detailblog = get_object_or_404(Blog, pk=blog_id)
    return render(request, 'blog/detail.html', {'blog':detailblog})

好的,所以根据这个我应该去details.html,它有

{{blog.title}}

但是除了404错误之外我什么也看不见!

0 个答案:

没有答案