python - 在 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 不工作后请帮我解决这个问题。
解决方案
(
拆前支架slug
改变这个
path('post/(<slug:slug>/', views.posts_detail,name='post'),
至
path('post/<slug:slug>/', views.posts_detail,name='post'),
谢谢
推荐阅读
- automated-tests - 让 TestCafe 识别 dotenv 变量
- sql-server - Azure SQL Server 上是否有一种日志来检查某些函数何时创建/删除?
- c - 如何使用 C 在 Windows 和 Linux 上模拟单击?
- python - 正则表达式:查找字母 numeirc 子字符串,但不严格按字母顺序排列,可能是完全数字
- javascript - this.$set 更新数组不起作用(VueJS)
- javascript - 在节点 js 中导入默认值
- sabre - Sabre静态数据
- symfony - Sylius:如何不执行一些固定装置,但保持原版文件不变?
- laravel - Laravel DB::select 返回一般错误:2031
- java - 如何将数组值放入 dbSqlite 上的数据库?