CreateView怪异的行为。观点有误?

时间:2019-05-22 13:43:45

标签: python django django-views

在我的urls.py中,我有一个ListView和一个CreateView。当我在url模式中同时拥有两个视图时,CreateView将显示链接到ListView的html。但是,当我从网址格式中删除ListView时,CreateView将显示正确的html。

urls.py

如果我这样,CreateView将显示ListView html

.hour-glass1 {
  position: relative;
  display: flex;
  justify-content: center;

  img {
    width: 200px;
    position: absolute;
    animation-name: multiple-image-crossfade;
    animation-timing-function: ease-in-out;
    animation-iteration-count: infinite;
    animation-duration: 3s;
    &:nth-of-type(1) {
      transform: scaleY(-1);
      animation: showhidespin 3s linear infinite;
    }
    &:nth-of-type(2) {
      animation-delay: 1.8s;
    }
    &:nth-of-type(3) {
      animation-delay: 1.2s;
    }
    &:nth-of-type(4) {
      animation-delay: 0.6s;
    }
    &:nth-of-type(5) {
      animation-delay: 0;
    }
  }
}

@keyframes multiple-image-crossfade {
  0% {
    opacity:1;
  }
  15% {
    opacity:1;
  }
  20% {
    opacity:0;
  }
  92% {
    opacity:0;
  }
  100% {
    opacity:1;
  }
}

@keyframes showhidespin {
  0% { opacity: 0 }
  75% { opacity: 0 }
  80% { opacity: 1; transform: rotate(0deg); }
  100% { opacity: 1; transform: rotate(180deg); }
}

这样,CreateView的行为就像我想要的那样。显示正确的HTML

urlpatterns = [
    path("", views.TopicListView.as_view(), name="topic"),
    path("<topic>/", views.PostListView.as_view(), name="post"),
    path("create/", views.CreatePostView.as_view(), name="create_post")
]

views.py

urlpatterns = [
    path("", views.TopicListView.as_view(), name="topic"),
    path("create/", views.CreatePostView.as_view(), name="create_post")
]

1 个答案:

答案 0 :(得分:3)

URL的原因是从上到下逐一检查。因此,当您有3个网址时:

urlpatterns = [
    path("", views.TopicListView.as_view(), name="topic"),
    path("<topic>/", views.PostListView.as_view(), name="post"),
    path("create/", views.CreatePostView.as_view(), name="create_post")
]

并尝试使用create/,它实际上与<topic>/模式匹配,并且已传递了字符串。所以我建议将其放到最下面:

urlpatterns = [
    path("", views.TopicListView.as_view(), name="topic"),
    path("create/", views.CreatePostView.as_view(), name="create_post")
    path("<topic>/", views.PostListView.as_view(), name="post"),
]

但是如果您将一些其他路径添加到url中,这样也不会像list/<topic>/这样笨拙,那会更好。