【问题标题】:how to get different UI elements by the choicefield value?如何通过选择字段值获取不同的 UI 元素?
【发布时间】:2015-05-03 17:14:15
【问题描述】:

这是我的代码,test.html if 语句有问题。您能帮我修改test.html 中的if 条件,以便我可以通过选择字段值获取不同的UI 元素吗?

forms.py

class testForm(forms.Form):
    runtype = forms.ChoiceField(choices=[(0, 0), (1, 1)])
    byname = forms.CharField()
    byindex = forms.IntegerField()

views.py

@csrf_exempt
def osys(request):
    if request.method == 'POST':
        form = testForm(request.POST, request.FILES)
        if form.is_valid():
            runtype = form.cleaned_data['runtype']
            if runtype == 0:
                byname = form.cleaned_data['byname']
            if runtype == 1:
                byindex = form.cleaned_data['byindex']
            print byname, byindex, runtype, filename
            return HttpResponse('OK')
    else:
        form =  testForm()
    return render_to_response('test.html', locals())

test.html

<form method="post" >
    {{form.runtype}}
    {% if form.runtype ==0 %}
        {{form.byname}}
    {% if form.runtype ==1 %}
        {{form.byindex}}
    <input type="submit" value="Start Run" name="btnRun"></input>
</form>

【问题讨论】:

  • 您需要关闭模板中的if 语句。

标签: django filter choicefield


【解决方案1】:

Django 模板在服务器端呈现为纯 HTML,然后发送到不知道模板代码的客户端。

如果您需要在客户端动态执行操作,则需要使用 JavaScript。

【讨论】:

    猜你喜欢
    • 2023-02-02
    • 2021-02-26
    • 2018-03-12
    • 1970-01-01
    • 2012-12-23
    • 2018-12-29
    • 2015-10-15
    • 1970-01-01
    • 2015-07-28
    相关资源
    最近更新 更多