Django NameError名称'request'未定义

时间:2018-05-08 13:23:54

标签: python django django-views

收到错误Django NameError名称'request'未定义。

我的views.py

class ShiftCreateView(CreateView):
    fields = ('name', 'timein', 'timeout', 'desc')
    model = models.Shift

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.timein = request.POST.get("timein", "")
        self.object.timeout = request.POST.get("timeout", "")
        self.object.save()


        return super(ModelFormMixin, self).form_valid(form)

1 个答案:

答案 0 :(得分:4)

在CBV内部,可以从对象self获取请求:

self.object.timein = self.request.POST.get("timein", "")
self.object.timeout = self.request.POST.get("timeout", "")