收到错误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)
答案 0 :(得分:4)
在CBV内部,可以从对象self
获取请求:
self.object.timein = self.request.POST.get("timein", "")
self.object.timeout = self.request.POST.get("timeout", "")