0

I've created combine ListView and FormView. If I invoking form valid method everything works fine, but if form is not valid and I try to initialize form_invalid method and redirect to MainView to display form errors, I get error.

views.py

class MainView(ListView, FormMixin):
    """
    Main which display all posts
    """
    #  Setting template name
    template_name = 'post/main.html'
    #  Setting view model
    model = Post
    # Setting pagination
    paginate_by = 5
    #  Setting form class
    form_class = PostForm

    #  Overriding get_context_data method
    def get_context_data(self, **kwargs):
        context = super(MainView, self).get_context_data(**kwargs)
        context['form'] = self.form_class()
        context['trends'] = Tag.objects.all().order_by('posts')
        return context

    #  Handling post sending
    def post(self, request):
        form = self.form_class(request.POST)

        if form.is_valid():
            self.form_valid(form)
            return redirect('main')
        else:
            self.form_invalid(form)
            return redirect('main')

    def form_valid(self, form):
        #  Getting body text from form
        text = form.cleaned_data['body']
        #  Getting actual logged in user
        author = self.request.user
        #  Getting image file
        image_file = form.cleaned_data['image_file']
        #  Getting image_url
        image_url = form.cleaned_data['image_url']
        #  Creating post instance and saving
        post = Post(text=text, author=author, image_file=image_file, image_url=image_url)
        post.save()

error

AttributeError at /main/

'MainView' object has no attribute 'object_list'

post/main.html

    <!--Post column-->
    <section class="col-sm-7">

        <!--Iterating through posts-->
        {% for post in object_list %}
        {{ post.text}}
        {% endfor %}

    </section>

5
  • Does swapping around the parents' names help? class MainView(FormMixin, ListView):. Commented Jan 15, 2016 at 17:11
  • Seems like you're trying to access object_list attribute from your view instead of using it as template variable. Can you show the template 'post/main.html'..? Commented Jan 15, 2016 at 18:33
  • @mariodev Okay, I added it. Commented Jan 15, 2016 at 19:36
  • @AlexMorozov I did it and seems to have no effect. Commented Jan 15, 2016 at 19:41
  • @PawełKosiński Can you please include full traceback if possible.. Thanks! Commented Jan 15, 2016 at 19:52

1 Answer 1

1

The problem is the ListView and the FormMixin don't play well together out-of-box, so you need to change your view a bit. Try this sample base view:

class CreateListView(CreateView, ListView):
    def form_invalid(self, request, *args, **kwargs):
        return self.get(self, request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = CreateView.get_context_data(self, **kwargs)
        context.update(
            super(FormListView, self).get_context_data(**kwargs)
        )
        return context


class MainView(CreateListView):
    template_name = 'post/main.html'
    model = Post
    paginate_by = 5
    form_class = PostForm
    success_url = reverse_lazy('this-view-name')

    def get_context_data(self, **kwargs):
        context = super(MainView, self).get_context_data(**kwargs)
        context['trends'] = Tag.objects.all().order_by('posts')
        return context
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.