我制作了这个网络应用程序 https://notes12345.herokuapp.com
这个应用程序的代码在这里:https://github.com/theparadoxer02/Notes
想要的是以这样一种方式制作搜索表单:当用户选择年份时,分支的选择会动态生成,这意味着当用户选择分支后,分支选项应该出现在所选年份的选项中,那么应该出现与上面所选年份、分支相关的科目选项。 所以我想用这种方式生成 form 。
我应该学什么?我应该在哪里修改views.py或models.py中的文件?我被困住了。
这是我的模型文件:
year_choices = (
( 1 , 'First' ),
( 2 , 'Second'),
( 3 , 'Third' ),
( 4 , 'Fourth')
)
branch_choices = (
( 'IT','IT' ),
( 'EE','EE' ),
( 'CSE','CSE'),
( 'EC','EC' ),
( 'ME','ME' ),
( 'CE','CE' ),
)
subject_choices = (
( 'DS' , 'Data Structure' ),
( 'OS' , 'Operating sytem' ),
( 'EC' , 'Ecomomics' ),
( 'Thermo' , 'Thermo' ),
)
def generate_picture_name(instance, filename):
url = "images/{0}_{1}_{2}.jpg".format(
instance.subjects.branch, instance.subjects.year, instance.unit_no)
return url
class Subject(models.Model):
name = models.CharField(max_length=20)
no_of_units = models.IntegerField()
year=models.IntegerField(choices=year_choices)
branch=models.CharField(choices=branch_choices,max_length=15)
def __str__(self):
return self.name
class Note(models.Model):
#unit_choices = ((1,'1'),(2,'2'),(3,'3'),(4,'4'),(5,'5'),(6,'6'),(7,'7'),(8,'8'),(9,'9'),(10,'10'))
#branch = models.CharField(max_length=55,choices=branch_choices)
#year = models.IntegerField(choices = year_choices)
#subject_name = models.CharField(choices=subject_choices,max_length=10)
subjects = models.ForeignKey(Subject,on_delete=models.CASCADE)
unit_no = models.IntegerField()
picture = models.ImageField(upload_to = generate_picture_name)
def __str__(self):
return str(self.id)
最佳答案
您可以使用 intercooler.js 轻松完成此操作特别是其 dependent Select文档中提到的功能。
关于python - django 中的动态表单选择/选项字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42739399/