[Fixed]-Post request with Django DetailView gives error 'MyView' object has no attribute 'object'

20πŸ‘

βœ…

What you are missing here is that you have to assign the object to the class or self before calling the get_context_data().

class Detail(DetailView):
    model = MyModel
    template_name = 'mymodel_detail.html'

    def get_context_data(self, **kwargs):
        context = super(Detail, self).get_context_data(**kwargs)
        context['form'] = DetailForm
        return context

    def post(self, request, *args, **kwargs):
        form = DetailForm(request.POST, request.FILES)
        if form.is_valid():
            # Write Your Logic here

            self.object = self.get_object()
            context = super(Detail, self).get_context_data(**kwargs)
            context['form'] = DetailForm
            return self.render_to_response(context=context)

        else:
            self.object = self.get_object()
            context = super(Detail, self).get_context_data(**kwargs)
            context['form'] = form
            return self.render_to_response( context=context)

and in render_to_response() Just pass context. No other arguments.

Hope it will work for you.

πŸ‘€Sarfraz Ahmad

5πŸ‘

This is how I implemented the code from Safrazs answer to make a reply option on my question model. I know this is an old question, but I hope that someone will find this useful.

class QuestionDetailView(generic.DetailView):
    model = Question
    template_name = 'forum/question.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['form'] = ReplyForm

        return context

    def post(self, request, *args, **kwargs):
        form = ReplyForm(request.POST)

        if form.is_valid():
            reply = form.save(commit=False)
            reply.creator = request.user
            reply.question = self.get_object()
            reply.save()
            self.object = self.get_object()
            context = context = super().get_context_data(**kwargs)
            context['form'] = ReplyForm

            return self.render_to_response(context=context)

        else:
            self.object = self.get_object()
            context = super().get_context_data(**kwargs)
            context['form'] = form

            return self.render_to_response(context=context)

4πŸ‘

You are inheriting from the wrong generic view. You need to inherit from CreateView, like this:

class CreateModel(CreateView):
  model = MyModel
  template_name = 'mymodel_detail.html'
  form_class = DetailForm
  success_url = reverse('/thanks')

  def form_valid(self, form):
     # this method is called when the form
     # is successfully validated
     # if you need to do something with
     # the database object, this is the place
     # do not use it to redirect to a success page
     # use the success_url for that
     return super(CreateModel, self).form_valid(form)
πŸ‘€Burhan Khalid

1πŸ‘

You are calling super on the wrong class: they should be Detail, not MessageDetail. Also, you don’t need the form code. Instead, use one of the generic editting views (CreateView, DeleteView, FormView, UpdateView). The DetailView is only for display purposes really. More detail on the generic views can be found at http://ccbv.co.uk/

πŸ‘€hellsgate

Leave a comment