knightzoid
knightzoid

Reputation: 157

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.

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

Upvotes: 1

Views: 411

Answers (1)

John Carter
John Carter

Reputation: 55271

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

Upvotes: 2

Related Questions