使用Django DetailView的Post请求给出错误'MyView'对象没有属性'object'

k97glaaz  于 2023-10-21  发布在  Go
关注(0)|答案(4)|浏览(96)

我正在尝试使用django DetailView创建一个对象。我的代码就是这样。

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():
            context['reply_form'] = DetailForm
            self.object = super(Detail, self).get_object()
            context['object'] = super(Detail, self).get_object()

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

            return self.render_to_response(request=request, template=self.get_template_names(), context=context)

但在这里,我得到的错误,

'Detail' object has no attribute 'object'

我试着在上下文示例中分配对象,也用self。但都不管用。

s6fujrry

s6fujrry1#

在这里,您忽略了在调用get_context_data()之前必须将对象分配给类或self。

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)

render_to_response()中,只需传递上下文。没有其他论点。
希望它能为你工作。

ut6juiuv

ut6juiuv2#

这就是我如何实现Safrazs answer的代码,以便在我的问题模型上创建一个回复选项。我知道这是一个老问题,但我希望有人会发现这是有用的。

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

z4bn682m3#

您从错误的泛型视图继承。你需要从CreateView继承,像这样:

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

xa9qqrwz4#

你在错误的类上调用了super:应该是Detail,而不是MessageDetail。另外,您不需要表单代码。相反,使用一个通用的编辑视图(OpenView,DeleteView,FormView,UpdateView)。DetailView实际上仅用于显示目的。有关通用视图的更多详细信息,请访问http://ccbv.co.uk/

相关问题