编辑-__init __()获得了意外的关键字参数'instance'

时间:2019-05-21 12:16:34

标签: django django-forms django-views

我正在尝试为表单创建“编辑”。

urls.py

url(r'^app_1/(?P<id>[-\w]+)/edit/$',views.edit, name = 'edit'),

forms.py

class ClanakForma(forms.ModelForm):
class Meta:
    model = Clanak
    fields = '__all__'

models.py

class Clanak(models.Model):
    naslov = models.CharField(null=False, blank=True, max_length=120)
    datumObjave = models.DateField(null=False, blank=False)
    autor = models.CharField(null=False, blank=True, max_length=50)
    email = models.EmailField(max_length=75, null=True, blank=True)

    def __str__(self):
        return str(self.naslov) + ', ' + str(self.datumObjave) + ', ' + str(self.autor)

views.py

def edit(request, id):
    data = get_object_or_404(Clanak, id = id)
    if request.method == "POST":
        form = ClanakForma(request.Clanak, instance=data)
        if form.is_vaild():
            data = form.save(commit=False)
            data.naslov = request.user
            data.datumObjave = request.user
            data.autor = request.user
            data.email = request.user
            return redirect('readAllNew')
    else:
        form = ClanakForma(instance=data)
        template = 'readAllNew.html'
        context = {'form': form}
        return render(request, template, context)

readAllNew.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<table border="1">

        <tr>
        <th>Naslov</th>
        <th>Datum</th>
        <th>Autor</th>
        <th>Mail</th>
        </tr>
        {% for x in data %}
    <tr>
        <td>{{x.naslov}}</td>
        <td>{{x.datumObjave}}</td>
        <td>{{x.autor}}</td>
        <td>{{x.email}}</td>
        <td><a href="{% url 'delete' x.id %}">delete</a></td>
        <td><a href="{% url 'edit' x.id %}">edit</a></td>
    </tr>
    {% endfor %}
</table>
</body>
</html>

因此,当我在“ readAllNew.html”上点击编辑链接时,我会收到错误消息:

  / p_1 / 5 / edit /
处的

TypeError    init ()获得了意外的关键字参数'instance'

我认为我的观点有问题,但是我不确定是什么。

-------------------------------更新--------------- -----------------

根据建议,我编辑了“ forms.py”,但现在看不到任何表单字段,因此无法进行任何编辑:

See screenshot

----------------更新2 -------------------------

我创建了新的html文件为“ edit.html”

我编辑了“ views.py”:

def edit(request, id):
    data = get_object_or_404(Clanak, id = id)
    if request.method == "POST":
        form = ClanakForma(instance=data)
        if form.is_vaild():
            data = form.save(commit=False)
            data.naslov = request.user
            data.datumObjave = request.user
            data.autor = request.user
            data.email = request.user
            return redirect('readAllNew.html')
    else:
        form = ClanakForma(instance=data)
        template = 'edit.html'
        context = {'form': form}
        return render(request, template, context)

现在我可以正确看到其中的字段和信息,但是当我更改某些内容并单击“提交”时,我会收到错误消息:

AttributeError at /app_1/6/edit/
'ClanakForma' object has no attribute 'is_vaild'

2 个答案:

答案 0 :(得分:2)

使用forms.ModelForm代替 forms.Form

class Forma(forms.ModelForm):
    naslov = forms.CharField(label='naslov')
    datumObjave = forms.DateField(label='datumObjave')
    autor = forms.CharField(label='autor')
    email = forms.EmailField(label='email')

    class Meta:
        model = Clanak
        fields = ('naslov', 'datumObjave', 'autor', 'email')

如果要在表单中使用 Model类的所有字段 ,则可以指定 fields 属性 Meta 类作为 fields = '__all__'

#example
class Forma(forms.ModelForm):
    class Meta:
        model = Clanak
        fields = '__all__'


如果您使用的是 forms.ModelForm ,则可以避免大多数实现,从而使事情变得容易:)


更新

您需要在模板中呈现表单。因此,将模板更改为

<form method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit">
</form>

答案 1 :(得分:0)

您不能将instance传递为简单格式-它必须为ModelForm。使用这个:

class Forma(forms.ModelForm):
    class Meta:
        model = Clanak
        fields = ['naslov', 'datumObjave', 'autor', 'email']
        labels = {
            'naslov': 'naslov',
            'datumObjave': 'datumObjave',
            'autor': 'autor',
            'email': 'email'
        }