如果路线以' post /'开头,则Django与CreateView不匹配

时间:2017-07-09 20:22:28

标签: python regex django routing views

我在django写了一个小博客网站。我使用通用视图和各种工作。除了CreatePostView,它是一个通用的CreateView:

class CreatePostView(LoginRequiredMixin, generic.CreateView):
    model = Post

    login_url = '/login/'
    redirect_field_name = 'blog_app/post_detail.html'

    form_class = PostForm

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.author = self.request.user
        self.object.save()
        return super().form_valid(form)

我将它通过blog.urls发送到blog_app.urls

# Project urls
url(r'^blog/', include('blog_app.urls'), name='blog'),

# App urls
url(r'^post/new/$', views.CreatePostView.as_view(), name='post_new'),

但是当我去/ blog / post / new /我会得到

Page not found (404)
    Request Method: GET
    Request URL:    http://127.0.0.1:8000/blog/post/new/
    Raised by:  blog_app.views.PostDetailView

然而,这将有效:

 url(r'^new/post/$', views.CreatePostView.as_view(), name='post_new'),

现在,如果我去

http://127.0.0.1:8000/blog/new/post/ 

我看到了我的表格。

无论我在斜线后写什么,如果第一部分是' post /'它会404我。至少我认为我观察到了这一点。 它也是相反的方向:

url(r'^somesuperlongroute/add/$',
    views.CreatePostView.as_view(), name='post_new'),

这是有效的,但这应该是怎样的。

有什么特别的关于发布'在路线的第一部分?我有5条其他路线以' post /'开头哪个工作,实际上我可能只是在这里包含这个文件:

from django.conf.urls import url, include
from . import views

app_name = 'blog_app'

urlpatterns = [
    url(r'^$', views.PostListView.as_view(), name='post_list'),
    url(r'^about/$', views.AboutView.as_view(), name='about'),
    url(r'^post/(?P<slug>[-\w]+)/$',
        views.PostDetailView.as_view(), name='post_detail'),
    url(r'^post/(?P<slug>[-\w]+)/edit/$',
        views.PostUpdateView.as_view(), name='post_edit'),
    url(r'^post/new/$',
        views.CreatePostView.as_view(), name='post_new'),
    url(r'^drafts/$', views.DraftListView.as_view(), name='post_draft_list'),
    url(r'^post/(?P<slug>[-\w]+)/remove/$',
        views.PostDeleteView.as_view(), name='post_remove'),
    url(r'^post/(?P<slug>[-\w]+)/publish/$',
        views.post_publish, name='post_publish'),
    url(r'^post/(?P<slug>[-\w]+)/comment/$',
        views.add_comment_to_post, name='add_comment_to_post'),
    url(r'^comment/(?P<pk>\d+)/approve/$',
        views.comment_approve, name='comment_approve'),
    url(r'^comment/(?P<pk>\d+)/remove/$',
        views.comment_remove, name='comment_remove'),
]

有什么想法吗?我可以提供您喜欢的模型,视图和表格,但我发现它们并不相关。

1 个答案:

答案 0 :(得分:2)

Django将使用第一个匹配的正则表达式,在本例中为Bar1<- ggplot(ContinentMaster,aes(x=ContinentMaster$Continent,y=NoOfcountries, fill=NoOfcountries,label = NoOfcountries,na.rm = TRUE)) Bar1+ geom_bar(stat = "identity",color="Blue",fill="LightGreen",size=0.5)+ geom_text(size=3, position = position_stack(vjust = 1.05))+ xlab(" ")+ ylab("No Of Countries")+ scale_y_continuous(limits=c(0, 60),breaks=c(0,10,20,30,40,50,60))+ scale_x_discrete(labels = function(x) str_wrap(x, width = 10))+ theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), axis.line = element_line(colour = "black")) 。这会以r'^post/(?P<slug>[-\w]+)/$'的标记路由到PostDetailView。由于您没有该帖子的帖子,视图将会引发404.

要解决此问题,只需将new置于CreatePostView

之上
PostDetailView