django + jquery +和动态表单,字段发送空

时间:2009-08-08 19:22:38

标签: jquery django django-forms

我不知道有多恶意这么说,但我有一个Form,在这个表单中我试图添加一些字段用jquery(动态??)点击一个单选按钮,所以,当我尝试发送数据时,django说字段是空的......并且在debuger(错误页面??)中是tru,字段是空的,但是我的Form.py中的字段不是。

我刚接触Django,但我认为如果字段进入数据会发送......好吧Django是个大世界。

Form.py

class DiligenciaForm(ModelForm):
""" this is my unhidden form with the radios buttons """

    titulo = forms.CharField(max_length=70,help_text='Dele un nombre a su diligencia.')
    tipo = forms.ChoiceField(choices=TIPO)  
    vias= forms.TypedChoiceField(widget=forms.RadioSelect(), choices=CHOICES)

    class Meta:
        model = Diligencia
        #exclude =('socio','secuencia','ffin','fecha','fentrada','status')
        fields = ('titulo', 'tipo','vias')


#now the hidden form's that i want to render (append) to the first Form

class UnaViaForm(forms.Form):
""" i want with jquery show this form (append to) """
    Desde = forms.CharField(max_length=100)
    Hasta = forms.CharField(max_length=100)

View.py

来自diligencia.account.forms import DiligenciaForm,UnaViaForm

@login_required
def test(request):
    form = NuevaDiligenciaForm()
    form_1_via = UnaViaForm()

    return render_to_response('account/vias-form.html', { 'formulario':form,'UnaVia':form_1_via },
                                                           context_instance = RequestContext(request))

嗯,这是一种奇怪的方式:在我的模板中,我有这个代码:

<div id="Una_via"> {{ UnaVia.as_p }} </div> 

and the jquery:
$(document).ready(function(){

   $("#Una_via").css("display","none");

   $("input[name=vias]").click(function(){ 
        var radio = $("input[name='vias']:checked").val();
        if(radio == 1){
            $("#Una_via").show("fast");
        }else{ 
            $("#Una_via").hide("fast"); } 
    });
  });

1 个答案:

答案 0 :(得分:1)

假设此表单POST回到同一视图,您忘记将表单绑定到POST数据:

def test(request):
    data = request.method == 'POST' and request.POST or None
    form = NuevaDiligenciaForm(data=data)
    form_1_via = UnaViaForm(data=data)