我有一个简单的观点:
def admin_videos(request):
if request.method=="POST":
print(request.POST)
else:
a = Note.objects.get(pk=1)
form = NoteForm(instance=a)
return render(request, 'administration/index.html',{"form":form})
和一个模型:
class NoteForm(ModelForm):
authors = ModelMultipleChoiceField(queryset=User.objects.all())
class Meta:
model = Note
fields = '__all__'
widgets = {
'note': Textarea(attrs={'class': "form-control note", 'rows': 5}),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if 'instance' in kwargs:
self.fields['note'].widget.attrs['data-id'] = kwargs['instance'].id
但是当我尝试更新现有实例时:
<form method="POST" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit"/>
</form>
实例的id不在request.POST中 我很感激任何建议