1
我是django的新手。我需要根据用户在表单中选择的单选按钮将条件下拉列表放到我的Web应用程序中。这个想法是,基于用户的单选按钮选择,应该在表单上填充另一个下拉列表。此下拉列表将从数据库中检索的值列表中。从此下拉菜单中选择的最终值将通过按下不同的按钮来完成主要操作。请建议如何做到这一点。一个例子或链接肯定会有所帮助。由于Django - 如何在数据库的模板中添加条件下拉菜单
我的示例代码
<script>
function my_radio_select() {
var1 = document.getElementById("radio1");
var2 = document.getElementById("radio2");
var3 = document.getElementById("radio3");
if (var1.checked === true) {
window.location.href = "A.html";
}
else if(var2.checked === true) {
window.location.href = "A.html";
document.myform.action = "A.html";
}
else if(var3.checked === true) {
window.location.href = "A.html";
}
}
</script>
<br>
<br>
<br>
<input type="radio" id="radio1" name="project_type" value=0 checked onclick="my_radio_select();">
<label>Projects I own</label>
<br>
<input type="radio" id="radio2" name="project_type" value=1 onclick="my_radio_select();">
<label>Projects I manage</label>
<br>
<input type="radio" id="radio3" name="project_type" value=1 onclick="my_radio_select();">
<label>Projects I can edit</label>
{% endblock %}
class MyProjForm(ModelForm):
existing_projs = CSProj.objects.all()
choices_int = tuple((p.name, p.name) for p in existing_projs)
#tech = forms.ChoiceField(label='Existing Projects' ,widget=forms.Select(attrs={'class':'form-control'}),choices=choices_int,required=True)
tech = forms.ChoiceField(label='Existing Projects' ,widget=forms.Select(attrs={'class':'form-control'}),choices=choices_int,required=False)
class Meta:
model = CSProj
fields = ['name','user_workspace', 'compiler_path','ccs_path','tdk_path']
exclude = ('name','user_workspace', 'compiler_path','ccs_path','tdk_path')
def __init__(self, *args, **kwargs):
if 'user' in kwargs:
user = kwargs.pop('user')
super(MyProjForm, self).__init__(*args, **kwargs)
### Done to refresh choices as soon as it is added
existing_projs = CSProj.objects.filter(owner_id=user)
choices_int = tuple((p.name, p.name) for p in existing_projs)
self.fields['tech'].choices=choices_int
嗨,谢谢你的回复。我对这个领域很陌生。我没有想法。如果你能说出一些肯定会有所帮助的例子。感谢您回复 – user3364086
@ user3364086只有在您满意答案后才接受答案!否则他们只会离开它.. –
请显示你的表格,标记你想要出现的字段,如果单选按钮被选中 - 我会给你一个例子。 – chem1st