NameError:name' name'未在django视图上使用request.POST.get定义

时间:2018-05-11 11:11:16

标签: python django-views

我试图从帖子中获取数据。

models.py

class Employee(models.Model):
    name = models.CharField(max_length=100)

views.py

class Seller(CreateView):
    fields = (name,)
    model = models.Seller

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.name = self.request.POST.get("name", "")+ "additional string" #concatenate string
        self.object.save()

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

我的模板html

{{ form.as_p }}
来自cmd的

错误

NameError: name 'name' is not defined

1 个答案:

答案 0 :(得分:1)

我不是django专家,但如果您尝试更改此内容,该怎么办:

fields = (name,)

到那个:

fields = ('name',)