我正在尝试创建一个CreateView,该视图在Django中注册种族的一系列详细信息。
views.py:
$( window ).mousemove(function( event ) {
var $el = $('#draggable');
var $window=$( window ).height();
var $bottom = $el.position().top + 100 ;
var $top = $window - 100;
if ($bottom > $window ){
$('#draggable').css({ top: $top });
}
});
models.py:
class RegistrarDetalles(CreateView):
model = DetallesCarrera
second_model = Carrera
form_class = RegistrarDetallesForm
template_name = "carrera/detallesC.html"
success_url = reverse_lazy('detalleC')
def get_context_data(self, **kwargs):
pk = self.kwargs.get('pk')
context = super(RegistrarDetalles, self).get_context_data(**kwargs)
context['carrera'] = Carrera.objects.get(id=pk)
return context
def get_success_url(self):
return reverse_lazy('detalleC', kwargs={'pk': self.object.id_carr})
forms.py:
class DetallesCarrera(models.Model):
id = models.CharField(max_length=20, primary_key=True)
numero = models.IntegerField(blank=True, null=True)
id_carr = models.ForeignKey(Carrera, on_delete=models.CASCADE)
id_caba = models.ForeignKey(Caballo, on_delete=models.CASCADE)
id_jock = models.ForeignKey(Jockey, on_delete=models.CASCADE)
posicion = models.IntegerField(blank=True, null=True)
urls.py:
class RegistrarDetallesForm(forms.ModelForm):
class Meta:
model = DetallesCarrera
fields = [
'id',
'id_carr',
'id_caba',
'id_jock',
]
widgets = {
'id': forms.TextInput(attrs={'class': 'form-control'}),
'id_carr': forms.Select(attrs={'class': 'form-control','id':'uno'}),
'id_caba': forms.Select(attrs={'class': 'form-control'}),
'id_jock': forms.Select(attrs={'class': 'form-control'}),
}
html模板中的表单:
path('listaCarr/<pk>/detalleC/agregar', login_required(views.RegistrarDetalles.as_view()), name='RegDetallCr'),
碰巧,当我访问页面本身时,数据会正常加载,并且可以执行所需的选择,如下所示,但是当我单击“ Incluir”按钮时,它什么也没有做,显然只是重新加载了页面无需插入。
它没有显示Posicion和Numero的输入,因为我希望它们在不同的视图中进行更新。