Python 为什么Django试图打开一个我不知道的URL';你不告诉它吗?
大家好强> 我正在用Django创建一个小博客,其中只有一个应用程序。碰巧我已经定义了博客的很大一部分,这是:Python 为什么Django试图打开一个我不知道的URL';你不告诉它吗?,python,html,django,web,Python,Html,Django,Web,大家好 我正在用Django创建一个小博客,其中只有一个应用程序。碰巧我已经定义了博客的很大一部分,这是: 主视图 每个出版物类别的视图 每个帖子的视图 除其他外 现在我想添加“About Author”视图,当它应该重定向到各自的HTML模板时,Django最终将自己重定向到另一个模板,这会生成一个NoReverseMatch错误 简化代码,这是: from django.shortcuts import render from django.views.generic import Li
- 主视图
- 每个出版物类别的视图
- 每个帖子的视图
- 除其他外
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post, Author, Category
class Home(ListView):
def get(self, request, *args, **kwargs):
context = {
'post': Post.objects.get(title='NamePost')
}
return render(request, 'PageWebApp/home.html', context)
class PostSimple(DetailView):
def get(self, request, slug_post, *args, **kwargs)
context = {
'post': Post.objects.filter(slug_post=slug_post)
}
return render(request, 'PageWebApp/page-simple.html', context)
class PostsCategory(DetailView):
def get(self, request, category, *args, **kwargs):
# View that shows each of the categories within the blog
context = {
'categories': Category.objects.get(category=category)
}
return render(request, 'PageWebApp/posts-category.html', context)
class AboutAuthor(DetailView):
def get(self, request, slug_autor, *args, **kwargs):
context = {
'author': Author.objects.get(slug_author=slug_author)
}
return render(request, 'PageWebApp/page-author.html', context)
视图。py:
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post, Author, Category
class Home(ListView):
def get(self, request, *args, **kwargs):
context = {
'post': Post.objects.get(title='NamePost')
}
return render(request, 'PageWebApp/home.html', context)
class PostSimple(DetailView):
def get(self, request, slug_post, *args, **kwargs)
context = {
'post': Post.objects.filter(slug_post=slug_post)
}
return render(request, 'PageWebApp/page-simple.html', context)
class PostsCategory(DetailView):
def get(self, request, category, *args, **kwargs):
# View that shows each of the categories within the blog
context = {
'categories': Category.objects.get(category=category)
}
return render(request, 'PageWebApp/posts-category.html', context)
class AboutAuthor(DetailView):
def get(self, request, slug_autor, *args, **kwargs):
context = {
'author': Author.objects.get(slug_author=slug_author)
}
return render(request, 'PageWebApp/page-author.html', context)
url.py
from django.contrib import admin
from django.urls import path
from PageWebApp import views
urlpatterns = [
path ('', views.Home.as_view (), name = 'home'),
# [Here are the URLs to the other project templates (they work fine)]
# Next the conflictive ones:
path ('posts- <category> /', views.PostsCategory.as_view (), name = 'posts-category'),
path ('<slug: slug_post> /', views.PostSimple.as_view (), name = 'page-simple'),
path ('about-<slug: slug_author> /', views.AboutAuthor.as_view (), name = 'page-author'),
]
来自django.contrib导入管理
从django.url导入路径
从PageWebApp导入视图
URL模式=[
路径(“”,views.Home.as_view(),name='Home'),
#[这里是指向其他项目模板的URL(它们工作正常)]
#接下来是有冲突的:
路径('posts-/',views.PostsCategory.as_view(),name='posts category'),
路径('/',views.PostSimple.as_view(),name='page simple'),
路径('about-/',views.AboutAuthor.as_view(),name='page author'),
]
我有一个名为“base.html”的模板,其他所有人都继承它
在名为“home.html”的启动模板中,我们可以实现以下功能:
<! - HERE GO OTHER TAGS THAT REDIRECT TO OTHER URLS ->
<h4> <a href="{% url 'posts-category' categories.category %}"> See posts from {{categories.category}} </a> </h4>
<h4> <a href="{% url 'page-simple' post.slug_post %}> {{post.title}} </a> </h4>
<h4> <a href="{% url 'page-author' author.slug_author %}> By: {{author.name}} </a> </h4>
正如我前面提到的,当进入主窗口“home.html”时,我有一系列“a”标记重定向到各种模板,但特别是当我选择转到page-author.html模板的URL时,出于某种原因,Django解释说它应该重定向到页面类别,并给出了所描述的错误
Reverse for 'posts-category' with arguments '('',)' not found. 1 pattern(s) tried: ['posts\\-(?P<category>[^/]+)/$']
未找到参数为“(“”,)”的“帖子类别”的反向。尝试了1个模式:[“posts\\-(?P[^/]+)/$”]
我已经彻底审查了每个HTML模板,它们都正确地重定向到相应的URL
提前感谢您的回复和评论。首先,您的路径中是否有空格?我不知道这是否会破坏任何东西 不管怎样,我认为这与你的路径设置方式有关。此路径正在捕获每个路径
路径('/'
)。这样,它下面的所有路径都首先被该路径捕获,因为它们与该模式匹配。在示例中,您可以做的是更改如下顺序:
path ('posts- <category> /', views.PostsCategory.as_view (), name = 'posts-category'),
path ('about-<slug: slug_author> /', views.AboutAuthor.as_view (), name = 'page-author'),
path ('<slug: slug_post> /', views.PostSimple.as_view (), name = 'page-simple'),
path('posts-/',views.PostsCategory.as_view(),name='posts-category'),
路径('about-/',views.AboutAuthor.as_view(),name='page author'),
路径('/',views.PostSimple.as_view(),name='page simple'),
这可能会起作用,但最干净的方法是根据a/like区分路径:
path ('posts/<category>/category/', views.PostsCategory.as_view (), name = 'posts-category'),
path ('posts/<slug: slug_post>/', views.PostSimple.as_view (), name = 'page-simple'),
path ('authors/<slug: slug_author>/', views.AboutAuthor.as_view (), name = 'page-author'),
path('posts//category/',views.PostsCategory.as_view(),name='posts category'),
路径('posts/',views.PostSimple.as_view(),name='page simple'),
路径('authors/',views.AboutAuthor.as_view(),name='page author'),