如何将参数从模板传递到视图django

时间:2013-06-18 11:27:28

标签: django forms django-templates

我想在单击提交按钮

时将模板中的下拉变量传递给函数
    #models.py :-

    class donnee(models.Model):
        name = models.CharField(_('name'), max_length= 150)

        def __unicode__(self):
            return self.name

        class Meta:
            verbose_name = _('donnee')
            verbose_name_plural = _('donnees filtrage')
            ordering = ['name']


    class Property(models.Model):
        name = models.CharField(_('name'), max_length=50)
        description = models.TextField(_('description'), blank=True)

        def __unicode__(self):
            return self.name

        class Meta:
            verbose_name = _('property')
            verbose_name_plural = _('properties')
            ordering = ['name']


    class Physic2(models.Model):
        name = models.ForeignKey(Property, verbose_name=_('name'), null=True, blank=True)
        lapropriete = models.ForeignKey(donnee, verbose_name=_('lapropriete'),  blank=True)
        #lapropriete = models.CharField(_('property'), max_length=100)
        description = models.TextField(_('description'), blank=True)

        def __unicode__(self):
            return self.lapropriete

        class Meta:
            verbose_name = _('physic2')
            verbose_name_plural = _('physics2')
            ordering = ['name']

    #forms.py:-

    class Physic2Form(forms.ModelForm):
        class Meta:
            model = Physic2

    #views.py:-

    def get_materials_one(request, category_slug=None):
        if category_slug is None:
            lafamille= 'general'
            propriete= Physic2Form()
            return render_to_response('material/critere1.html',
                                       {'unefamille': lafamille,
                                        'propriete': propriete},
                                         context_instance=RequestContext(request))

    #template:-

    <form id= "testjson" action="{% url chercher_filtre1 %}" method= "get"  onsubmit = "">

    {{ propriete.lapropriete }}

    <td><input type="submit" style="background-color:#D7D8D4;" value="Search" id= "chercher"/></td>
    </div>

    </form>

    #function:-

    valT1 = request.GET['lapropriete'] 

this don't work

当我点击下拉列表中的字段时

valT1 = 1或2 ......它只是字段的id

我有字段

维氏硬度(GPa) 剪切模量(GPa) 年轻模量(GPa)

请求什么.GET [??]

2 个答案:

答案 0 :(得分:1)

您可以这样写并尝试。

if request.method == 'GET':
    form = Physic2Form(request, data=request.GET)
    if form.is_valid():
        data = form.cleaned_data
        valT1 = data['lapropriete']

答案 1 :(得分:0)

我的功能

def search_filter1(请求):     尝试:

    val_min1 = float(request.GET['Vmin1'])
    val_max1 = float(request.GET['Vmax1'])
    T_min1 = float(request.GET['Tmin1'])
    T_max1 = float(request.GET['Tmax1'])
    if request.method == 'GET':
        form = Physic2Form(request.GET)
        if form.is_valid():
            valT1 = form['lapropriete']
    print 'val_min1:',val_min1
    print 'val_max1:',val_max1
    print 'Tmin1:', T_min1
    print 'Tmax1:', T_max1
    print 'valT1:',valT1

如果我在点击提交按钮时选择属性'Shear Modulus(GPa)',  我明白了:

val_min1:44.0 val_max1:99.0 Tmin1:44.0 Tmax1:99.0

valT1: <select name="lapropriete" id="id_lapropriete">
<option value="">---------</option>
<option value="2">Elasticity Modulus (GPa)</option>
<option value="4" selected="selected">Shear Modulus (GPa)</option>
<option value="1">Vickers Hardness (GPa)</option>
<option value="3">Young Modulus (GPa)</option>
</select>

为什么?