简体   繁体   English

是否可以使用基于类的视图而不是基于函数的视图 wagtail?

[英]is it possible to use class based view instead of function based view wagtail?

i'm still struggling to integrate django wagtail to an existing project.我仍在努力将 django wagtail 集成到现有项目中。

i'm only using wagtail for my blog page.我只在我的博客页面上使用 wagtail。 and i want to create a form to create new post for my blog from my wagtail page.我想创建一个表单来从我的 wagtail 页面为我的博客创建新帖子。 the way i create this is using an routablepage.我创建它的方式是使用路由页面。 here's some of my code这是我的一些代码

i'm using this as my reference我用这个作为我的参考

models.py模型.py

class BlogIndex(RoutablePageMixin, Page):
    ...

    @route(r'^send-post/$', name='send_posts')
    def submit(self, request):
        from .views import submit_news
        return submit_news(request, self)
    ...

class BlogPage(Page):
    ...

forms.py表格.py

class NewsPageForm(forms.ModelForm):
    ...

views.py视图.py

def submit_blog(request, blog_index):
    ...

is it possible to change submit_blog function into create view ?是否可以将 submit_blog 功能更改为创建视图? because i've tried to make create view before and try something like this but it doesn't work because it will recursive to call the BlogPage Page in models.py因为我之前尝试过创建视图并尝试类似的操作,但它不起作用,因为它会递归调用 models.py 中的 BlogPage 页面

models.py模型.py

class BlogIndex(RoutablePageMixin, Page):
...

    @route(r'^send-post/$', BlogCreate.as_view(), name='send_posts')

views.py视图.py

class BlogCreate(CreateView):
...

thank you very much非常感谢您

I think you're nearly there, but @route needs to decorate a view function (rather than passing the view as a decorator parameter).我想你@route ,但@route需要装饰一个视图函数(而不是将视图作为装饰器参数传递)。

Try this:尝试这个:

class BlogIndex(RoutablePageMixin, Page):
...
    @route(r'^send-post/$', name='send_posts'):
    def submit(self, request):
        blog_create_view = BlogCreate.as_view()

        return blog_create_view(request, self)

instead of:代替:

class BlogIndex(RoutablePageMixin, Page):
...

    @route(r'^send-post/$', BlogCreate.as_view(), name='send_posts')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM