我无法将表单数据保存到数据库中,每次尝试时,都会收到不同的错误。
这是我的表单模型
class Meta:
model = Asistente
exclude = ('evento',)
widgets = {'nombre': forms.TextInput(attrs={'class': 'form-control'}),
'telefono': forms.TextInput(attrs={'class': 'form-control'}),
'num_asistentes': forms.TextInput(attrs={'class': 'form-control'})}
这是我的观点的代码。
def post(self, request, evento_id):
evento = Evento.objects.filter(id=evento_id).first()
form = RegistroAsistenteForm(request.POST)
if form.is_valid():
registro_asistente = form.save(commit=False)
registro_asistente.evento.pk = evento_id
registro_asistente.save()
else:
form = RegistroAsistenteForm()
return HttpResponseRedirect('/evento-reg/' + str(evento_id) + '/asistente/')```
There are times when I don't get any error, it just don't do anything.
您的视图应该看起来
def post(request, evento_id):
if request.method == 'POST':
evento = Evento.objects.filter(id=evento_id).first()
form = RegistroAsistenteForm(request.POST)
if form.is_valid():
registro_asistente.evento = evento
form.save()
#to get the id u can us form.instance.id
return redirect('to what ever u want from you urls')
else:
form = RegistroAsistenteForm()
return HttpResponseRedirect('/evento-reg/' + str(evento_id) + '/asistente/')```
这就是收集你想在视图中做什么,它不需要自己