我在我的博客应用程序中创建一个创建 View 来创建帖子。在里面 创建 View 我使用了函数 get_success_url。我想要当我创作的时候 一个帖子,它将重定向到 blog_post_list。
我收到错误:NoReverseMatch
我想它必须与 urlpatterns 相关。
main urls.py
from django.conf.urls import url, include
from django.contrib import admin
from blog.views import AboutPageView, ContactPageView
urlpatterns = [
url(r'', include('blog.urls', namespace='posts')),
url(r'^blog/', include('blog.urls', namespace='posts')),
url(r'^about/$', AboutPageView.as_view(), name='about'),
url(r'^contact/$', ContactPageView.as_view(), name='contact'),
#admin and login
url(r'^admin/', admin.site.urls),
]
博客应用中的网址
from django.conf.urls import url
from .views import blog_postListView, blog_postDetailView, blog_postCreateView
urlpatterns = [
url(r'^$', blog_postListView.as_view(), name='blog_post_list'),
url(r'^create/', blog_postCreateView.as_view(), name='blog_post_create'),
url(r'^(?P<slug>[-\w]+)$', blog_postDetailView.as_view(), name='detail'),
]
博客应用中的浏览量
class blog_postCreateView(CreateView):
#model = blog_post
form_class = blog_postForm
template_name = "form.html"
#fields = ["title", "content"]
def get_success_url(self):
return reverse("blog_post_list")
最佳答案
您尚未包含命名空间,因此无法找到 blog_post_list
所以只需在反向调用中添加命名空间即可
反向(“帖子:blog_post_list”)
有关 NoReverseMatch
错误的详细信息,请参阅 What is a NoReverseMatch error, and how do I fix it?
关于django - NoReverseMatch Django,CreateView 中的 get_success_url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39741129/