简体   繁体   English

从另一个下拉列表填充Django下拉列表-Javascript

[英]Populate Django Drop Down List from another Drop Down List - Javascript

I have a Django + Python website, where user can request a access to a database. 我有一个Django + Python网站,用户可以在其中请求访问数据库。 So in this form the user has these options. 因此,以这种形式,用户可以选择这些选项。

Environment: This is a drop down list with two values: Development and Production 环境:这是一个具有两个值的下拉列表:开发和生产
Permission: This is another drop down list with two values: Read and Read/Write 权限:这是另一个具有两个值的下拉列表:读取和读取/写入

What I am trying to achieve is: 我想要实现的是:

If Environment == Development
   AddToPermissionDropDownList: Database Owner
else
 Permission

So, as you can see, I want to populate one drop down list based in the selected value of another drop down. 因此,如您所见,我想根据另一个下拉列表的选定值来填充一个下拉列表。

I know that I need to use JavaScript to achieve that, but I am stuck on how to do it. 我知道我需要使用JavaScript来实现这一目标,但是我仍然坚持如何做到这一点。

This is what I have been able to do so far. 到目前为止,这是我能够做到的。

models.py models.py

class NewAccessRequest(models.Model):
permission_needed = models.CharField(max_length=25,verbose_name='Permissions')

forms.py 表格

class AccessRequestForm(ModelForm):
environment= forms.ChoiceField(choices=ENVIRONMENT_CHOICES,label="",initial='',widget=forms.Select(attrs={'id':'environment','onchange':'changePermissions()'}), required=True)
permission_needed = forms.ChoiceField(initial='',widget=forms.Select(), required=True)

access.html access.html

<form>
{% csrf_token %}
   <label for="{{ form.environment.id_for_label }}">Environment:</label>
    <p>{{form.environment}}</p>
<script>
function changePermissions() {
var EnvChosen = document.getElementById("environment").value;
if (EnvChosen == "DEV") {
document.getElementById("id_business_reason").value = "This is a test"

        }
    }
    </script>
</form>

Since it needs to be populated from the options from back end . 由于需要从后端的选项中进行填充。 you can call a ajax method to get the data from back end on the change event of dropdown-1 , then you have to populate dropdown-2 with data got from the response of the ajax , also you have to pass the value from dropdown-1 with the request. 您可以调用ajax方法在dropdown-1的change事件上从后端获取数据,然后必须使用从ajax响应获得的数据填充dropdown-2,还必须传递dropdown-的值1与请求。 you can either use a get or post request. 您可以使用get或post请求。 if it is post request you also need to pass csrf token 如果是后期请求,则还需要传递csrf令牌

This is what I have been able to do so far. 到目前为止,这是我能够做到的。 The only problem that I am seeing is. 我看到的唯一问题是。

1) In the environment drop down list I select: DEV 1.1) DB OWNER gets added 2) Now change the environment drop down list to: PROD 2.1) DB READER gets added 3) Now change the environment drop down list to: DEV 3.1) DB OWNER gets added again. 1)在环境下拉列表中,我选择:DEV 1.1)添加了DB OWNER 2)现在将环境下拉列表更改为:PROD 2.1)添加了DB READER 3)现在将环境下拉列表更改为:DEV 3.1) DB OWNER被再次添加。

I know that I need somehow to cleanup the values in the drop down list, I just don't know yet on where and what code to do. 我知道我需要某种方式来清理下拉列表中的值,我只是不知道在哪里以及要执行什么代码。

<script>
function changePermissions() {
   var EnvChosen = document.getElementById("environment").value;
   var selectList = document.getElementById("id_permission_needed")

   if (EnvChosen == "DEV") {
        var option = document.createElement("OPTION");
        var txt = document.createTextNode("DB OWNER");
        option.appendChild(txt);
        selectList.insertBefore(option, selectList.lastChild);
       }
   if (EnvChosen == "PROD") {
         var option = document.createElement("OPTION");
         var txt = document.createTextNode("DB READER");
         option.appendChild(txt);
         selectList.insertBefore(option, selectList.lastChild);
              }
          }
    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM