我无法将表单数据保存到数据库中,每次尝试时,都会收到另一个错误。
这是我的表单模型
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.
答案 0 :(得分:0)
您的视图应外观
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/')```
这就是在视图中收集您想做的事情的过程,它不需要自我