gpt4 book ai didi

python - 单击下拉选项并在 Django 中显示特定字段

转载 作者:行者123 更新时间:2023-11-28 02:31:07 25 4
gpt4 key购买 nike

我有以下情况,我有这个模型:

class Profile(models.Model):

DEVELOPER = 1
MARKETER = 2
ACADEMIC = 3
INFLUENCER = 4
COMMUNITY_MEMBER = 5

ROLE_CHOICES = (
(DEVELOPER, 'Developer'),
(MARKETER, 'Marketer'),
(ACADEMIC, 'Academic'),
(INFLUENCER, 'Influencer'),
(COMMUNITY_MEMBER, 'Community Member'),
)

user = models.OneToOneField(User)
account_type = models.ForeignKey(AccountType, default=AccountType.FREE)
role = models.PositiveIntegerField(choices=ROLE_CHOICES, null=True, blank=True)
github_profile = models.URLField(blank=True, null=True)
linkedin_profile = models.URLField(blank=True, null=True)

现在我想要做的是,如果用户在下拉列表中单击 Developer 选项,github_profile 应该出现,否则它应该保持隐藏状态。

现在,看起来像这样,Github 链接字段应该被隐藏:

enter image description here

这是我的 html:

  <div class="form-group select-wrapper">
<label for="role">What best describes you?</label>
<select id="role" name="role" class="form-control">
<option selected>Choose an option</option>
{% for index, value in roles_form.fields.roles.choices %}
<option value="{{ index }}">{{ value }}</option>
{% endfor %}
</select>
<span class="role-error hidden" style="color: red"></span>
</div>
<div class="form-group">
<label for="github_profile">GitHub Profile (optional)</label>
<input type="url" class="form-control" name="github_profile" id="github_profile" placeholder="https://github.com/your-profile">
<span class="github_profile-error hidden" style="color: red"></span>
</div>

我知道这是有条件的事情,但是我是 Django 的新手,我不太确定如何实现它。

编辑:

我做了以下功能:

$('select').on('change', function() {

var value = $(this).val();
if (value == 1) {
$('#github_link').show();
} else if (value != 1) {
$('#github_link').hide();
}
})

它有效,但我得到以下反馈:这里您硬编码了值“1”,但如果此列表中的位置发生变化,则此行为将中断。相反,您应该引用变量 {{ model.profile.DEVELOPER }},它将产生变量 DEVELOPER 的当前值,即使它发生变化也是如此。

最佳答案

我认为实现这一目标的唯一方法是使用 Javascript。例如在这个例子中使用 jQuery:https://stackoverflow.com/a/26589859/5698557

关于python - 单击下拉选项并在 Django 中显示特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50746932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com