PostCreateView 返回由 PostDetailView 引发的页面错误 404
PostCreateView returning Page Error 404 raised by PostDetailView
我正在尝试在我的 Django 项目中添加一个 CreateView
,但我收到 PostDetailView
引发的页面 404 错误,出于某种原因我不明白为什么。我假设这是因为 url 模式,但我已经修改了几次但仍然卡住了。
这是完整的错误:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/score/new_design/
Raised by: score.views.PostDetailView
这是Models.py
class Post(models.Model):
title = models.CharField(max_length=100, unique=True)
design = models.ImageField(blank=False, null=True, upload_to=upload_design_to)
这是view.py
class PostCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
model = Post
fields = ['title', 'design']
success_url = '/'
template_name = 'score/post_form.html' # <app>/<model>_<viewtype>.html
success_message = "Your Post has been submitted"
def form_valid(self, form):
form.instance.designer = self.request.user
return super().form_valid(form)
这是urls.py
urlpatterns = [
path('', PostListView.as_view(), name='score'),
path('<slug:slug>/', PostDetailView.as_view(), name='post-detail'),
path('new_design/', PostCreateView.as_view(), name='post-create'),
这是创建视图的link:
<a class="dropdown-item" href="{% url 'score:post-create' %}">New Post</a>
您可以尝试将创建的内容 url 移动到详细信息上方,如下所示:
urlpatterns = [
path('', PostListView.as_view(), name='score'),
path('new_design/', PostCreateView.as_view(), name='post-create'),
path('<slug:slug>/', PostDetailView.as_view(), name='post-detail'),
这允许首先找到创建视图。或者您尝试像这样编辑详细信息 url:
urlpatterns = [
path('', PostListView.as_view(), name='score'),
path('detail/<slug:slug>/', PostDetailView.as_view(), name='post-detail'),
path('new_design/', PostCreateView.as_view(), name='post-create'),
问题在于,由于详细视图首先出现,因此它正在搜索要匹配的有效 slug,但由于找不到它而出错
我正在尝试在我的 Django 项目中添加一个 CreateView
,但我收到 PostDetailView
引发的页面 404 错误,出于某种原因我不明白为什么。我假设这是因为 url 模式,但我已经修改了几次但仍然卡住了。
这是完整的错误:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/score/new_design/
Raised by: score.views.PostDetailView
这是Models.py
class Post(models.Model):
title = models.CharField(max_length=100, unique=True)
design = models.ImageField(blank=False, null=True, upload_to=upload_design_to)
这是view.py
class PostCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
model = Post
fields = ['title', 'design']
success_url = '/'
template_name = 'score/post_form.html' # <app>/<model>_<viewtype>.html
success_message = "Your Post has been submitted"
def form_valid(self, form):
form.instance.designer = self.request.user
return super().form_valid(form)
这是urls.py
urlpatterns = [
path('', PostListView.as_view(), name='score'),
path('<slug:slug>/', PostDetailView.as_view(), name='post-detail'),
path('new_design/', PostCreateView.as_view(), name='post-create'),
这是创建视图的link:
<a class="dropdown-item" href="{% url 'score:post-create' %}">New Post</a>
您可以尝试将创建的内容 url 移动到详细信息上方,如下所示:
urlpatterns = [
path('', PostListView.as_view(), name='score'),
path('new_design/', PostCreateView.as_view(), name='post-create'),
path('<slug:slug>/', PostDetailView.as_view(), name='post-detail'),
这允许首先找到创建视图。或者您尝试像这样编辑详细信息 url:
urlpatterns = [
path('', PostListView.as_view(), name='score'),
path('detail/<slug:slug>/', PostDetailView.as_view(), name='post-detail'),
path('new_design/', PostCreateView.as_view(), name='post-create'),
问题在于,由于详细视图首先出现,因此它正在搜索要匹配的有效 slug,但由于找不到它而出错