使用 Django DetailView 发布请求给出错误“MyView”对象没有属性“object”

问题描述 投票:0回答:4

我正在尝试使用 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 中分配对象。但没有任何作用。

django django-forms django-views
4个回答
20
投票

这里缺少的是,在调用

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()
中只需传递上下文即可。没有其他论据。

希望它对你有用。


5
投票

这就是我实现 Safrazs 答案中的代码以在我的问题模型上做出回复选项的方式。我知道这是一个老问题,但我希望有人会发现这很有用。

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
投票

您继承了错误的通用视图。你需要继承

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)

1
投票

您在错误的班级中调用

super
:他们应该是
Detail
,而不是
MessageDetail
。此外,您不需要表单代码。相反,请使用通用编辑视图之一(CreateView、DeleteView、FormView、UpdateView)。 DetailView 实际上仅用于显示目的。有关通用视图的更多详细信息,请访问 http://ccbv.co.uk/

© www.soinside.com 2019 - 2024. All rights reserved.