【问题标题】:Add a "+" button that opens a popup inside my django model form添加一个“+”按钮,在我的 django 模型表单中打开一个弹出窗口
【发布时间】:2022-02-01 19:39:54
【问题描述】:

我想添加一个“+”按钮,在我的 django 模型表单中打开一个弹出窗口。对此有什么想法吗?

我在 models.py 中的代码

class library(models.Model):
    book_name = models.CharField(max_length=50, null=True, blank=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, limit_choices_to={'is_superuser': True}, null=True,
                               blank=True)
    description = models.TextField(null=True, blank=True)
    duration = models.DurationField(null=True, blank=True)
    date = models.DateField()
    book_image = models.ImageField(upload_to='lib/book_img', null=True, blank=True)

与作者字段平行,我想添加一个“+”按钮(添加新作者),以打开弹出模式表单。

我的 forms.py 是

class library_details(forms.ModelForm):
    class Meta:
        model = library
        fields = "__all__"

【问题讨论】:

标签: django django-forms modelform


【解决方案1】:

您可以使用引导模式。

https://getbootstrap.com/docs/4.0/components/modal/

您只需添加一个空白的表单字段,但设置打开模式所需的属性。

类似下面的东西

注意:我没有对此进行测试。

class library_details(forms.ModelForm):
    checkbox = forms.CheckboxInput()

    class Meta:
        model = library
        fields = "__all__"

    def __init__(self, *args, **kwargs):
        super(library_details, self).__init__(*args, **kwargs)
        self.fields['checkbox'].widget.attrs['data-toggle'] = "modal"
        self.fields['checkbox'].widget.attrs['data-target'] = "#exampleModal"

【讨论】:

    猜你喜欢
    • 2017-12-04
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 2022-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多