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

本小妞迷上赌 提交于 2019-12-13 02:26:33

问题


i'm still struggling to integrate django wagtail to an existing project.

i'm only using wagtail for my blog page. and i want to create a form to create new post for my blog from my wagtail page. the way i create this is using an routablepage. here's some of my code

i'm using this as my reference

models.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

class NewsPageForm(forms.ModelForm):
    ...

views.py

def submit_blog(request, blog_index):
    ...

is it possible to change submit_blog function into create view ? 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

class BlogIndex(RoutablePageMixin, Page):
...

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

views.py

class BlogCreate(CreateView):
...

thank you very much


回答1:


I think you're nearly there, but @route needs to decorate a view function (rather than passing the view as a decorator parameter).

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')


来源:https://stackoverflow.com/questions/52512545/is-it-possible-to-use-class-based-view-instead-of-function-based-view-wagtail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!