AttributeError'NarrateUpdate'对象没有属性'object'

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

我在NarrateUpdate视图上出现属性错误。我想编辑/更新来自表单的用户提交

这是我的views.py:

class NarrateUpdate(SuccessMessageMixin, UpdateView):
    model = Narrate
    fields = ['title', 'body']
    template_name = 'narrate_update_form.html'
    success_url = reverse_lazy('narrate-status')
    success_message = "Narration successfully updated"

    def get(self, request, *args, **kwargs):
        footer = FooterLinks.objects.all()
        context = self.get_context_data(**kwargs)
        context['footer'] = footer
        return self.render_to_response(context)

The urls.py

path('narration/<int:pk>/update/', NarrateUpdate.as_view(), name='edit-narration'),

这是models.py:

class Narrate(models.Model):

    STATUS = (
        ('F', 'FOR REVIEW'),
        ('P', 'PASSED'),
    )

    title = models.CharField(max_length=255)
    body = models.CharField(max_length=10000)
    status = models.CharField(
    max_length=25, choices=STATUS, default='for_review', blank=True)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return "/narration/%i/" % self.title

    class Meta:
        verbose_name = "narration"
        verbose_name_plural = "narrations"

我该怎么做才能使这项工作?

django
1个回答
0
投票

您为什么要覆盖get方法?如果您想更好地添加footer,请使用get_context_data方法:

class NarrateUpdate(SuccessMessageMixin, UpdateView):
    model = Narrate
    fields = ['title', 'body']
    template_name = 'narrate_update_form.html'
    success_url = reverse_lazy('narrate-status')
    success_message = "Narration successfully updated"

    def get_context_data(self, **kwargs):
        context = super(NarrateUpdate, self).get_context_data(**kwargs)
        context['footer'] = FooterLinks.objects.all()
        return context
© www.soinside.com 2019 - 2024. All rights reserved.