首页 > 解决方案 > 在 django 中找不到 slug 404

问题描述

这是我的意见.py

from .models import Post,Author


def posts_list(request):
   all_posts = Post.objects.all()
   context = {
       'all_posts': all_posts
   }
   return render(request,"post_list.html",context)


def posts_detail(request, slug):
   unique_post = get_object_or_404(Post, slug=slug)
   context = {
       'post': unique_post,
   }
   return render(request,"posts_detail.html",context)

我的 urls.py 是:

from django.urls import path
from posts import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('post/',views.posts_list),
    path('post/(<slug:slug>/', views.posts_detail,name='post'),
    ]

每次我去http://127.0.0.1:8000/post/first 我得到 404 页面没有找到我厌倦了重新加载我的 django 服务器并找到其他解决方案但我无法弄清楚https://http 的问题是什么: //127.0.0.1:8000/post路由正常,但是在 slug 不工作后请帮我解决这个问题。

标签: pythondjango

解决方案


(拆前支架slug

改变这个

path('post/(<slug:slug>/', views.posts_detail,name='post'),

path('post/<slug:slug>/', views.posts_detail,name='post'),

谢谢


推荐阅读